QVariant MyTableModel::data(const QModelIndex & index, int role) const{ switch(role) { ... case Qt::DecorationRole: return QIcon(...); } return QVariant();}
int main(int argc, char *argv[]){ QApplication app(argc, argv); QDirModel *model = new QDirModel; //QTreeView *tree = new QTreeView; //tree->setModel(model); QListView *tree = new QListView; tree->setModel(model); tree->setRootIndex(model->index(QDir::currentPath())); tree->setWindowTitle(QObject::tr("Dir View")); tree->resize(640, 480); tree->show(); return app.exec();}
QDir dir; dir.cd("C:/"); QStringList FileList=dir.entryList(); int listsize=FileList.size(); QDockWidget *somedock = new QDockWidget; QTreeView *view = new QTreeView; QStandardItemModel newitemmodel; for (int i = 0;i < listsize; ++i) { QStandardItem *item = new QStandardItem(QIcon("C:\install\icon.bmp"),FileList[i]); newitemmodel.appendRow(item); } view->setModel(&newitemmodel); view->setHeaderHidden(1); somedock->setWidget(view); addDockWidget(Qt::RightDockWidgetArea,somedock);