Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
[SOLVED] Can't set the window icon for QML application
I am trying to set the window icon for the QML application window (and to get it to display in the Windows taskbar). I have tried following the instructions "here":http://qt-project.org/doc/qt-5.0/qtdoc/appicon.html, but this doesn't work.
First of all, there is no setWindowIcon() method for the QtQuick2ApplicationViewer, only setIcon(). The documentation does not seem to mention this. But even if I use setIcon(), the Icon just isn't set, so I must be missing something. I've searched and searched, but can't seem to find a solution to this problem, only how to set the executable file icon (which was very helpful).
int main(int argc, char *argv)
QGuiApplication app(argc, argv);
QtQuick2ApplicationViewer viewer; viewer.setIcon(QIcon(":/icons/images/DESIGN.ico")); viewer.rootContext()->setContextProperty("QtQuick2ApplicationViewer", &viewer); viewer.rootContext()->setContextProperty("extFile", &extFile); viewer.setMainQmlFile(QStringLiteral("qml/Design2014Papers/main.qml")); viewer.showFullScreen(); viewer.setResizeMode(QtQuick2ApplicationViewer::SizeRootObjectToView); return app.exec();
The icon is included as a resource. The path to the icon was acquired by right clicking on the icon in the Resource file and copying the path to the clipboard and then pasting into the QIcon constructor.
Qt version 5.0.2 minGW
Platform: Win7 x64
I don't know if my solution is right but for me this is working:
In my project root I have file with icon (icon.ico) and file MyProjectName.rc with this line:
IDI_ICON1 ICON DISCARDABLE "icon.ico"
And in MyProjectName.pro I have:
RC_FILE = MyProjectName.rc
Hope it will help!
Edit: sorry, I've noticed that you have mentioned trying this solution, what exactly didn't work for you? I am using Win7 x64 with QT 5.0.2 as well and I have tried this and it works just fine for me. Icon is set for .exe file, shown in windows panel, and in window top left corner.
The icon is shown for the .exe file but it doesn't show in taskbar nor in the top left corner of the window. It does show if I make a plain Qt GUI application, but not if I make a Qt Quick (QML) application.
Additionally, setting the title using setTitle("bla") works fine. Does the icon need to have some specific properties regarding size, transparency etc?
I am also running QML app. Maybe there is something wrong with your ico file? I have created an empty QML qt quick 2 project with icon, you can see it "here":https://bitbucket.org/portoist/qml_icon_example/overview
"This is how it works for me":https://www.dropbox.com/s/i2jnsnbw6aa4898/example.PNG
Still can't get it to work. Are you running it from Qt Creator or directly from the .exe?
It should work from Qt Creator as well as from exe.
Did you try to use the ico file from test project I have posted inside your project? Or upload you icon somewhere and I can try to use it - so that we can check if there is something wrong with your icon, or if there something else.
I tried your icon with the same results, it displays for the .exe file but not for the window.
You can check my icon here:
In the meantime, could you upload your entire project somewhere so I can try it directly?
Your icon works fine for me, "this":https://www.dropbox.com/s/75h3hslb562trna/example2.PNG is what I can see.
I have created app just with hello world window and put it into bitbucket. You can download source code in zip from "here":https://bitbucket.org/portoist/qml_icon_example/get/76c4aa4b206d.zip
Just unzip it and open in Qt Creator, I have added your icon inside.
OK, I finally got it working. It looks like my .rc file was the problem with the name being too long.
I had this:
@IDI_DESIGN_ICON ICON DISCARDABLE "images/icon.ico"@
but the name can only have up to 8 characters, so I changed it to the name you put (IDI_ICON1) and now it works fine. Thanks a lot for your help. Love the use of nyan cat, by the way.
Edit: Maybe a note about this should be added to the documentation.
You are welcome, glad that it works.
I have never thought about changing the icon name to anything else, I have just took the line from Qt doc:-) I have played with it a bit and it seems that IDI_ICON1 is the only one working,
It works so I don't dare mess with it further. Marking this as solved.