How to display an icon on the top-left corner of a window, using CMake?
-
This is not a cmake problem. You can set the window icon with QApplication::setWindowIcon().
-
Does your new version of you app running on Qt 6.8 have your image as icon of the executable file (and also showing in TaskBar)?! Or is this also not working
Here is how you do it using CMake.
(How to set an icon to the window, like @Christian-Ehrlicher said above, is also mentioned there in the first few sentences) -
With your guidance, I was able to display my lab's icon in the top-left corner of the application window. Thank you!
To make sure that I have done this properly (and to ask a follow-up question), please allow me to list the changes that I have made.
Here is my revised CMakeLists.txt
cmake_minimum_required(VERSION 3.14) set(TARGET_NAME "MotherVoice") project(${TARGET_NAME} VERSION 0.12) set(CMAKE_PREFIX_PATH "C:/Qt/6.8.0/mingw_64") find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets Charts Multimedia) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) if(WIN32) set(WIN_ICON "AEPLab_icon/AEP_logo_3D_original.ico") elseif(APPLE) set(MAC_ICON "AEPLab_icon/AEP_logo_3D.icns") set_source_files_properties(${MAC_ICON} PROPERTIES MACOSX_PACKAGE_LOCATION "Resources") endif() add_executable(${TARGET_NAME} main.cpp mainwindow.cpp subjectwindow.cpp ../../FuhClasses/CWav.cpp ../../FuhClasses/FuhClasses.cpp ../../FuhClasses/dataMotherVoice.cpp ../../FuhClasses/recordMotherVoice.cpp ../../FuhClasses/entryMotherVoice.cpp resources.qrc ) target_sources(${TARGET_NAME} PRIVATE mainwindow.h subjectwindow.h ../../FuhClasses/CWav.h ../../FuhClasses/FuhClasses.h ../../FuhClasses/dataMotherVoice.h ../../FuhClasses/recordMotherVoice.h ../../FuhClasses/entryMotherVoice.h ../../../xResources/FFTReal/v2.00/Array.h ../../../xResources/FFTReal/v2.00/Array.hpp ../../../xResources/FFTReal/v2.00/def.h ../../../xResources/FFTReal/v2.00/DynArray.h ../../../xResources/FFTReal/v2.00/DynArray.hpp ../../../xResources/FFTReal/v2.00/FFTReal.h ../../../xResources/FFTReal/v2.00/FFTReal.hpp ../../../xResources/FFTReal/v2.00/FFTRealFixLen.h ../../../xResources/FFTReal/v2.00/FFTRealFixLen.hpp ../../../xResources/FFTReal/v2.00/FFTRealFixLenParam.h ../../../xResources/FFTReal/v2.00/FFTRealPassDirect.h ../../../xResources/FFTReal/v2.00/FFTRealPassDirect.hpp ../../../xResources/FFTReal/v2.00/FFTRealPassInverse.h ../../../xResources/FFTReal/v2.00/FFTRealPassInverse.hpp ../../../xResources/FFTReal/v2.00/FFTRealSelect.h ../../../xResources/FFTReal/v2.00/FFTRealSelect.hpp ../../../xResources/FFTReal/v2.00/FFTRealUseTrigo.h ../../../xResources/FFTReal/v2.00/FFTRealUseTrigo.hpp ../../../xResources/FFTReal/v2.00/OscSinCos.h ../../../xResources/FFTReal/v2.00/OscSinCos.hpp ) add_compile_definitions(QT_DISABLE_DEPRECATED_UP_TO=0x050F00) if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) endif() target_include_directories(${TARGET_NAME} PRIVATE ../../FuhClasses ../../../xResources/FFTReal/v2.00 ) target_link_libraries(${TARGET_NAME} PRIVATE Qt6::Core Qt6::Gui Qt6::Widgets Qt6::Charts Qt6::Multimedia ) install(FILES AEPLab_icon/AEP_logo_3D_original.ico AEPLab_icon/AEP_logo_3D.icns DESTINATION . )
I added the icon into a Qt Resource File (namely "resources.qrc"). Here is a screenshot of this Qt Resource File.
I used the setWindowIcon() to display an icon. Here is the script snippet that I created for this purpose.
setWindowIcon(QIcon(":/AEPLab_icon/AEP_logo_3D_original.ico"));
Here is a screenshot, showing that my lab's icon is displayed properly in the top-left corner of the window. Thank you!
Although the icon is displayed properly in the top-left corner of a window, this icon is NOT displayed as the icon of the executable file. That is, the icon of the MotherVoice.exe file is still a generic icon (instead of my lab's icon).
For clarity, here is a screenshot showing the generic icon of the *MotherVoice.exe"
What should I do to correct this mistake?
Any comments or suggestions will be appreciated!
-
You have to create a .rc file and add it to your sources.
E.g. here: https://forum.qt.io/topic/111141/setting-the-application-iconFor cmake you can simply add the .rc files to your sources. cmake is calling the correct programs afterwards.
-
I did add my lab's icon in a Qt Resource File, namely "resources.qrc" (as shown in a screenshot above). When I used QMake, I also had a .pro file, namely "MotherVoice.pro". In my MotherVoice.pro file, I did include a command (similar to the example that you referred to through an example link above). And yes. It worked when using a Qt Resource File ("resources.qrc") and a QMake .pro file ("MotherVoice.pro"). My lab's icon was displayed as the icon of the executable. That was correct.
However, I am now trying to use CMake (instead of QMake). First of all, do I have to use a regular .rc file (instead of a .qrc file) for CMake? Could this be the isue?
If a .qrc file is allowed with CMake, where should I go to specify the icon for the executable? On the other hand, if a .qrc file is not allowed with CMake, I would be happy to learn and create a regular .rc file for this project. I just need clarification so that I know how to proceed.
-
.rc and .qrc are two completely different things.
If you want an app icon in windows explorer you have to use a windows-specific .rc - file.
Ifyou want resources in your application (e.g. images) and want to include them directly into your executable, you can use the Qt qrc resource file.
And no, even qmake needed both.https://doc.qt.io/qt-6/qmake-variable-reference.html#rc-file
https://doc.qt.io/qt-6/qmake-variable-reference.html#resources -
Thank you so much for the clarification. I truly appreciate it!
Please allow me to reiterate, to ensure that I understand correctly:
In the Qt Creator IDE, if I switch to using CMake (i.e., using "CMakeLists.txt"), I MUST create a regular .rc file (instead of a .qrc file) and specify the icon of the executable within this .rc file. Correct?
-
As I already wrote it has nothing to do with qmake or cmake. If you want an application icon in windows explorer you have to add a .rc file. With qmake you might also set https://doc.qt.io/qt-6/qmake-variable-reference.html#rc-icons instead passing a rc file afair.
-
@beginner123 This has nothing to do with qrc. When using qmake, if you set RC_ICONS, qmake creates the rc file in the building folder and adds it to the auto-generated Makefile. But cmake doesn't provide such feature., so you need to create it and add it manually.
-
Thank you so much for the clarification. It helps a lot. This finally makes sense to me now.
I will spend some time to learn more about .rc files and to create one, as well as to manually add an icon to the executable.
I will be back onto this forum once I have made some progress. It may take me a while though, as I am just a part-time hobbyist in Qt programming.
Thanks again!