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. For a file, how to obtain a QImage of the icon shown on explorer?
Forum Updated to NodeBB v4.3 + New Features

For a file, how to obtain a QImage of the icon shown on explorer?

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 4.1k Views 2 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.
  • L lqsa

    When open explorer on Windows, an icon is shown for every file. How to obtain this icon in a QImage?

    I've tried:

    QFileIconProvider ip;
    QFileInfo info(fileName);
    QIcon icon = ip.icon(info);
    QPixmap pixmap = icon.pixmap(QSize(32, 32));
    QImage image = pixmap.toImage();
    

    but pixmap is always invalid

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

    @lqsa
    I do not know whether this will affect the result. But because of Windows Explorer icon generation and caching, I would make sure the you view the particular file used in the code in Windows Explorer, so you know the icon has been generated.

    Oh. I'm talking about thumbnail images for files like *.MP4, do you mean you see a different image for each file, or do you mean the generic icons Explorer uses for files, folders etc.?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lqsa
      wrote on last edited by lqsa
      #3

      No, I'm not talking about thumbnail images, I'm talking about file type icon or application icon if it's an exe.

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

        Hi,

        You can try the technique described here.

        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
        2
        • L Offline
          L Offline
          lqsa
          wrote on last edited by
          #5

          The same, pixmap is invalid.

          I'm not sure if the problem is in icon.pixmap method, because the ip.icon returns something on icon, and when call icon.availableSizes(), it returns a list with two QSize, 24x24 and 32x32.

          1 Reply Last reply
          0
          • hskoglundH Online
            hskoglundH Online
            hskoglund
            wrote on last edited by hskoglund
            #6

            Hi, just tried something similar to your code on my Windows 7; I created an empty vanilla widgets app and added this to mainwindow.cpp:

            #include "QHBoxLayout"
            #include "QLabel"
            #include "qdir.h"
            #include "qfileiconprovider.h"
            
            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
            
                auto b = new QHBoxLayout(ui->centralWidget);
                for (auto f : QDir("C:/Windows").entryInfoList(QDir::Files))
                {
                    auto icon = QFileIconProvider().icon(f);
                    auto l = new QLabel;
                    l->setPixmap(icon.pixmap(icon.availableSizes().first()));
                    b->addWidget(l);
                }
            }
            

            and got this:
            0_1510876955360_IconsGalore.png works ok :-)

            1 Reply Last reply
            2
            • L Offline
              L Offline
              lqsa
              wrote on last edited by
              #7

              I found it. The fileName was obtained from FileDialog, and it begins with file:///. After remove it, it works.

              Thank you very much.

              JonBJ 1 Reply Last reply
              0
              • L lqsa

                I found it. The fileName was obtained from FileDialog, and it begins with file:///. After remove it, it works.

                Thank you very much.

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

                @lqsa
                So OOI, your original call to QFileInfo info(fileName) did not indicate that the path was unacceptable?

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  lqsa
                  wrote on last edited by
                  #9

                  What method is used for this?

                  JonBJ 1 Reply Last reply
                  0
                  • L lqsa

                    What method is used for this?

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

                    @lqsa
                    That's just the constructor. But afterwards info.exists() would be a start if the file is supposed to exist, and it's not finding it via file:/// or whatever you said. Or I presume something in the information in the QFileInfo can't be right if it can't find the file or get its correct information...

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      lqsa
                      wrote on last edited by
                      #11

                      Yes, info.exists() returns false if not finding it.

                      JonBJ 1 Reply Last reply
                      0
                      • L lqsa

                        Yes, info.exists() returns false if not finding it.

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

                        @lqsa So that was your clue to the original problem :)

                        1 Reply Last reply
                        0

                        • Login

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