Icon on QPushButton does not show when running the application (Qt Widgets, CMake, .qrc)
-
Hello, everyone :)
I created a very simple Qt Widgets project in Qt Creator. Then I created a res.qrc file and added a .ico image from the img folder into it.
I also added the res.qrc file to my CMakeLists.txt. In Qt Creator's Design, I dragged a QPushButton onto the widget and set its icon using the image from the resource file.However, when I run the program, the button icon does not appear at all.
What could be the reason for this?Did I miss something when adding the .qrc resource file or setting the icon?
project dir
-.qtcreator
-build
-img
-- exit.png
-CMakeLists.txt
-dialog.cpp
-dialog.h
-dialog.ui
-main.cpp
-res.qrcCMakeLists.txt

res.qrc


dialog.ui


dialog.cpp
"qDebug() << icon.isNull();" always output true

main.cpp

-
@CheeseLatte
I think you have not added it correctly to CMakeLists.txt? For the.qrcI think you need qt_add_resources()?I don't know, but do you need to do this editing of the CMakeLists.txt manually? I don't use resources, but I assume Creator would sort out the CMakeLists.txt for you if you add a resource from inside it?
@JonB Thanks!
I think I’ve found the cause of the problem.
It turned out that RCC was not executed automatically, so the icons were not included in the build.
After addingset(CMAKE_AUTORCC ON)to my CMakeLists.txt and rebuilding the project, the icons started showing correctly.
Thanks again for your help :)
-
Hello, everyone :)
I created a very simple Qt Widgets project in Qt Creator. Then I created a res.qrc file and added a .ico image from the img folder into it.
I also added the res.qrc file to my CMakeLists.txt. In Qt Creator's Design, I dragged a QPushButton onto the widget and set its icon using the image from the resource file.However, when I run the program, the button icon does not appear at all.
What could be the reason for this?Did I miss something when adding the .qrc resource file or setting the icon?
project dir
-.qtcreator
-build
-img
-- exit.png
-CMakeLists.txt
-dialog.cpp
-dialog.h
-dialog.ui
-main.cpp
-res.qrcCMakeLists.txt

res.qrc


dialog.ui


dialog.cpp
"qDebug() << icon.isNull();" always output true

main.cpp

@CheeseLatte
I think you have not added it correctly to CMakeLists.txt? For the.qrcI think you need qt_add_resources()?I don't know, but do you need to do this editing of the CMakeLists.txt manually? I don't use resources, but I assume Creator would sort out the CMakeLists.txt for you if you add a resource from inside it?
-
@CheeseLatte
I think you have not added it correctly to CMakeLists.txt? For the.qrcI think you need qt_add_resources()?I don't know, but do you need to do this editing of the CMakeLists.txt manually? I don't use resources, but I assume Creator would sort out the CMakeLists.txt for you if you add a resource from inside it?
@JonB Thanks!
I think I’ve found the cause of the problem.
It turned out that RCC was not executed automatically, so the icons were not included in the build.
After addingset(CMAKE_AUTORCC ON)to my CMakeLists.txt and rebuilding the project, the icons started showing correctly.
Thanks again for your help :)
-
C CheeseLatte has marked this topic as solved on
-
@JonB Thanks!
I think I’ve found the cause of the problem.
It turned out that RCC was not executed automatically, so the icons were not included in the build.
After addingset(CMAKE_AUTORCC ON)to my CMakeLists.txt and rebuilding the project, the icons started showing correctly.
Thanks again for your help :)
@CheeseLatte
https://forum.qt.io/topic/143377/resources-management-in-qt-creator
Like the poster there, I assumed Creator would manage adding this sort of thing for cmake when you put resources into a project. Looks like it worked without manual intervention with qmake. -
@CheeseLatte
https://forum.qt.io/topic/143377/resources-management-in-qt-creator
Like the poster there, I assumed Creator would manage adding this sort of thing for cmake when you put resources into a project. Looks like it worked without manual intervention with qmake.@JonB
That's right ! I haven't used qmake. CMake does requireset(CMAKE_AUTORCC ON)to work, but there's a strange thing: the last time I created a project, I used CMake, and it loaded resources correctly even without "set(CMAKE_AUTORCC ON)". However, subsequent projects don't work that way. -
@JonB
That's right ! I haven't used qmake. CMake does requireset(CMAKE_AUTORCC ON)to work, but there's a strange thing: the last time I created a project, I used CMake, and it loaded resources correctly even without "set(CMAKE_AUTORCC ON)". However, subsequent projects don't work that way.@CheeseLatte
https://cmake.org/cmake/help/latest/prop_tgt/AUTORCC.html#prop_tgt:AUTORCCAUTORCC is a boolean specifying whether CMake will handle the Qt rcc code generator automatically, i.e. without having to use commands like qt4_add_resources(), qt5_add_resources(), etc. Currently, Qt versions 4 to 6 are supported.
So did your other case have an explicit qt_add_resources() like I wrote earlier? It looks like you can use one or the other. I am thinking Creator should perhaps insert an
qt_add_resources()when you add/create a resource file via its interface? -
@CheeseLatte
https://cmake.org/cmake/help/latest/prop_tgt/AUTORCC.html#prop_tgt:AUTORCCAUTORCC is a boolean specifying whether CMake will handle the Qt rcc code generator automatically, i.e. without having to use commands like qt4_add_resources(), qt5_add_resources(), etc. Currently, Qt versions 4 to 6 are supported.
So did your other case have an explicit qt_add_resources() like I wrote earlier? It looks like you can use one or the other. I am thinking Creator should perhaps insert an
qt_add_resources()when you add/create a resource file via its interface?@JonB
In another case, I didn’t useset(CMAKE_AUTORCC ON)orqt_add_resources(), and it still worked correctly.
When usingqt_add_executable(), it is not necessary to callqt_add_resources()explicitly — you can simply list the.qrcfile directly inqt_add_executable().