Important: Please read the Qt Code of Conduct -

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

  • 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

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

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

  • Lifetime Qt Champion


    You can try the technique described here.

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

  • 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) :
        ui(new Ui::MainWindow)
        auto b = new QHBoxLayout(ui->centralWidget);
        for (auto f : QDir("C:/Windows").entryInfoList(QDir::Files))
            auto icon = QFileIconProvider().icon(f);
            auto l = new QLabel;

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

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

    Thank you very much.

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

  • What method is used for this?

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

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

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

Log in to reply