For a file, how to obtain a QImage of the icon shown on explorer?
-
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...