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. getting the icon of a dropped file

getting the icon of a dropped file

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.3k 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.
  • P Offline
    P Offline
    phirestalker
    wrote on last edited by
    #1

    I am using qt5 and I have a qlistwidget that accepts drops. The drops work as I expect, now I would like to have the file or folder icon next to the path in the qlistwidgetitem. I see that it does accept an icon, I need to know how to get the icons. From looking through the autocomplete it looks like it is only a list of urls. What can I do to get the icons for the files?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      QFileIconProvider might be what you are looking for.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • P Offline
        P Offline
        phirestalker
        wrote on last edited by
        #3

        ok I found out how to get the icon, but I was under the impression that giving an icon in the constructor of a new qlistwidgetitem would make it display the icon next to the text? Is there some special way to make the icon appear?

        here is the relevant code
        void myQListWidget::dropEvent(QDropEvent *event)
        {
        qDebug() << "dropEvent";

        if (event->mimeData()->hasUrls())
         {
             QList<QUrl> urls = event->mimeData()->urls();
        
                 QUrl url;
                 foreach (url,urls)
                 {
                     QIcon icon(url.toLocalFile());
                     this->addItem(new QListWidgetItem( icon, url.toLocalFile()));
                 }
             event->acceptProposedAction();
        
         }
         QListWidget::dropEvent(event);
        

        }

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Did you check that the Icon is valid ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • P Offline
            P Offline
            phirestalker
            wrote on last edited by
            #5

            ya I was doing it wrong. Thanks I got it working, here is the code now

            void myQListWidget::dropEvent(QDropEvent *event)
            {
                qDebug() << "dropEvent";
            
                if (event->mimeData()->hasUrls())
                 {
                     QList<QUrl> urls = event->mimeData()->urls();
            
                         QUrl url;
                         foreach (url,urls)
                         {
                             QFileInfo file(url.toLocalFile());
                             QFileIconProvider iconProvider;
                             QIcon icon(iconProvider.icon(file));
                             this->addItem(new QListWidgetItem( icon, url.toLocalFile()));
                         }
                     event->acceptProposedAction();
            
                 }
                 QListWidget::dropEvent(event);
            }
            
            1 Reply Last reply
            1

            • Login

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