Qt 6.11 is out! See what's new in the release
blog
How to display icons in a QListView (Solution)
-
In order to display icons in a QListView, you must
1- set the list viewMode to IconMode
2- add your icons in a ressource
3- for each icon, create a pixmap, an item and use setData(Qt::DecorationRole, pixmap) to set the icon in the item;
4- add the item to the listMake sure to use the ":" instead of "qrc:" when referring to an icon. For example, use ":/Icons48x48/add.ico" instead of "qrc:/Icons48x48/add.ico". I don't know why but using "qrc" does not work for me !
void MainWindow::setupList() { setMenuIcon(":/Icons48x48/help.ico", "Help"); setMenuIcon(":/Icons48x48/add.ico", "More"); } void MainWindow::setMenuIcon(QString pixmapName, QString text) { QPixmap pixmap(pixmapName); QListWidgetItem* item = new QListWidgetItem(pixmap, text); item->setData(Qt::DecorationRole, pixmap); ui->lwIconsMenu->addItem(item); }Max2G
-
Hi and welcome to devnet,
There's a mistake, your explanations are for QListWidget, not QListView.