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
-
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.? -
Hi,
You can try the technique described here.
-
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:
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
That's just the constructor. But afterwardsinfo.exists()
would be a start if the file is supposed to exist, and it's not finding it viafile:///
or whatever you said. Or I presume something in the information in theQFileInfo
can't be right if it can't find the file or get its correct information...