• Hi,

    I have two questions regarding the QListWidget.

    1.) How can I check if an item is selected in the QListWidget?
    2.) How can I add icons to the items in the QListWidget like on the pictures on this site:


    Sorry, perhaps this is trivial, but I found no solutions.

  • Hi @magguz

    1. You can use the QListWidget signals : itemActivated(QListWidgetItem)*
      and itemEntered(QListWidgetItem)*

      then you can use bool QListWidgetItem::isSelected() const function.

    2. In the initialization of the QListWidget Item, for example:

    QListWidgetItem* item=new QListWidgetItem(QIcon(":/SEResources/logo.png"),conclusion);

  • @magguz

    From anywhere you have access to a QListWidget:

    1. QList <QListWidgetItem*> list = listWidget->selectedItems ();
      Also, from any slot that accepts a QListWidgetItem* you can check the isSelected () method.

    For example:

    void Xxx::on_list_itemSelectionChanged ()
    QList <QTableWidgetItem*> list = ui->sensors->selectedItems ();
    if (list.isEmpty ()) {

    1. For any QListWidgetItem* item, you can call item->setIcon (icon);
      Make sure, if you want larger icons to call setIconSize. This will allow for larger icons to be used without being scaled.

  • Hi Charlie_Hdz and Buckwheat,

    thanks for your replies. For the first question I prefer the solution of Buckwheat. The second question is a little bit more difficult. I use the QListWidget to display the contents of a directory on a ftp-server. Now I want at the beginning of each item an icon, depending if its a directory or just a file. It should looks similar to the Windows Explorer.

  • @magguz
    If you want the exact same icons as Explorer uses, IIRC something like shell32.dll file contains them. Or find equivalents elsewhere. Then use QListWidgetItem::setIcon().

    Also, have a look at https://stackoverflow.com/questions/38071572/qfilesystemmodel-with-ftp for what you're doing. There are some unfinished thoughts at https://forum.qt.io/topic/41438/thoughts-on-how-to-implement-ftpfilesystemmodel. And if you want to go as far as icons-per-ftp-file-type to make it look snazzy: https://stackoverflow.com/questions/11946601/qt-4-8-qfileiconprovider-getting-icon-for-non-existent-file-based-on-extensi :)

  • Hi @magguz

    You can use QFileIconProvider to get your icons to use. You can then get the icon from the provider using a QFileInfo object. For example:

    m_provider = new QFileIconProvider;


    QListWidgetItem* item = new QListWidgetItem (fileInfo.fileName ());
    item->setIcon (m_provider->icon (fileInfo);

    This will set the icon to the OS icon styles. No need to add your own unless you want a custom look.


    I derived from QFileIconProvider and QFileSystemModel to create my own file browser with some custom data management. The UI is a mixture of Fedora default browser and windows and designed for small device.
    If you can, have some fun and make your on FtpFileSystemModel and then attach the model to any of the views. This will make things really easy for your data management. If not, you can manually set like my sample code.

  • @magguz said in QListWidget:

    How can I check if an item is selected in the QListWidget?

    listWidget->selectionModel() gives you access to QItemSelectionModel::selectedIndexes() and QItemSelectionModel::selectionChanged signal

    How can I add icons to the items in the QListWidget

    • Using QListWidgetItem: item->setData(Qt::DecorationRole,QIcon("myicon.ico"));
    • Using QAbstractItemModel: listWidge->model()->setData(listWidget->model()->index(row,0),QIcon("myicon.ico"),Qt::DecorationRole)

Log in to reply