Have you read Model/View Programming and Model/View Tutorial?
Yes, it is possible to have an icon displayed with an entry in a model and control editing but not with QStringListModel (which presents an editable list of strings for things like combobox drop downs). To gain finer control you can use QStandardItemModel. The icon displayed is the value returned when QAbstractItemModel::data() is called with the role Qt::DecorationRole... by default null pixmap. Editability is controlled by the flags().
#include <QApplication>
#include <QStandardItemModel>
#include <QListView>
#include <QStyle>
#include <QIcon>
#include <QDir>
#include <QFileInfoList>
int main(int argc, char **argv)
{
const QIcon fileIcon
= qApp
->style
()->standardPixmap
(QStyle::SP_FileIcon);
const QIcon dirIcon
= qApp
->style
()->standardPixmap
(QStyle::SP_DirIcon);
QFileInfoList files
= QDir().
entryInfoList();
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
if (fi.isDir())
item->setData(dirIcon, Qt::DecorationRole);
else
item->setData(fileIcon, Qt::DecorationRole);
model.appendRow(item);
}
view.setModel(&model);
view.show();
return app.exec();
}
#include <QApplication>
#include <QStandardItemModel>
#include <QListView>
#include <QStyle>
#include <QIcon>
#include <QDir>
#include <QFileInfoList>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
const QIcon fileIcon = qApp->style()->standardPixmap(QStyle::SP_FileIcon);
const QIcon dirIcon = qApp->style()->standardPixmap(QStyle::SP_DirIcon);
QStandardItemModel model(0, 1);
QFileInfoList files = QDir().entryInfoList();
foreach(const QFileInfo &fi, files) {
QStandardItem *item = new QStandardItem(QString("%0").arg(fi.fileName()));
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
if (fi.isDir())
item->setData(dirIcon, Qt::DecorationRole);
else
item->setData(fileIcon, Qt::DecorationRole);
model.appendRow(item);
}
QListView view;
view.setModel(&model);
view.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks