简体   繁体   中英

How add icon in QListView using QStringListModel?

Is somehow possible to add icons to a ListView using a QStringListModel ?

This is what I'm doing.

QStringListModel* model;
QStringList List;
model->setStringList(List);
ui->listView->setModel(model);
...

model->setData(index, "Test");
model->setData(index,QIcon(":/icon.png"),Qt::DecorationRole);

unfortunately the icon does not appears on the list.

How can I add icons to the list?

QStringListModel does not support roles other than DisplayRole and EditRole .

Use QStandardItemModel instead in order to display icons via DecorationRole :

auto model = new QStandardItemModel(this);
ui->listView->setModel(model);
model->appendRow(new QStandardItem(QIcon(":/icon.png"), "Test"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM