Solved For a file, how to obtain a QImage of the icon shown on explorer?
-
No, I'm not talking about thumbnail images, I'm talking about file type icon or application icon if it's an exe.
-
Hi,
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) : 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
So OOI, your original call toQFileInfo info(fileName)
did not indicate that the path was unacceptable? -
What method is used for this?
-
@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... -
Yes, info.exists() returns false if not finding it.
-
@lqsa So that was your clue to the original problem :)