Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QListWidget
Forum Updated to NodeBB v4.3 + New Features

QListWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 5 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    magguz
    wrote on last edited by
    #1

    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:

    http://doc.qt.io/archives/qt-4.8/qlistwidget.html#details

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

    VRoninV 1 Reply Last reply
    0
    • C Offline
      C Offline
      Charlie_Hdz
      wrote on last edited by
      #2

      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);
      

      Kind Regards,
      Enrique Hernandez
      gearstech.com.mx
      chernandez@gearstech.com.mx

      1 Reply Last reply
      2
      • BuckwheatB Offline
        BuckwheatB Offline
        Buckwheat
        wrote on last edited by
        #3

        @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.

        Dave Fileccia

        1 Reply Last reply
        2
        • M Offline
          M Offline
          magguz
          wrote on last edited by
          #4

          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.

          JonBJ 1 Reply Last reply
          0
          • M magguz

            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.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @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 :)

            1 Reply Last reply
            0
            • BuckwheatB Offline
              BuckwheatB Offline
              Buckwheat
              wrote on last edited by
              #6

              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.

              0_1514926479956_884650dd-2588-4cd0-9e92-c7063252eb35-image.png

              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.

              Dave Fileccia

              1 Reply Last reply
              2
              • M magguz

                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:

                http://doc.qt.io/archives/qt-4.8/qlistwidget.html#details

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

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                @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)

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                2

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved