Problem with location of resources (svg icons) in subdir project with cmake
-
I've got the following project structure
<project root> ├── CMakeLists.txt └── src ├── app │ ├── CMakeLists.txt │ └── main.cpp ├── core │ ├── CMakeLists.txt │ ├── core.cpp │ └── core.h └── gui ├── CMakeLists.txt ├── mainwindow.cpp └── mainwindow.hThe top CMakeLists.txt reads
cmake_minimum_required(VERSION 3.16) project(seqfetch VERSION 1.0.0 LANGUAGES CXX) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(Qt6 REQUIRED COMPONENTS Widgets ) qt_standard_project_setup() add_subdirectory(src/core) add_subdirectory(src/ui) add_subdirectory(src/app)The app CMakeLists.txt reads
qt_add_executable(myapp main.cpp) target_link_libraries(mayapp PRIVATE Qt6::Widgets core gui) set_target_properties(myapp PROPERTIES WIN32_EXECUTABLE ON MACOSX_BUNDLE ON )The core CMakeLists.txt reads
qt_add_library(core STATIC core.h core.cpp) target_link_libraries(core PRIVATE Qt6::Core) target_include_directories(core INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} )An finally, the ui CMakeLists.txt reads
qt_add_library(gui STATIC mainwindow.h mainwindow.cpp) target_link_libraries(gui PRIVATE Qt6::Widgets core) target_include_directories(ui INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})The class
mainwindowis the only "library" using images (.svg) in the menus. But I wonder where to put these svg images in the project tree? Qt's manual is not very informative. In the "CMake Get started", more specifically in the "Adding resources" section, the manual explains that to add some pngs to the targethelloworldone should add the following to a CMakeLists.txt.qt_add_resources(helloworld imageresources PREFIX "/images" FILES logo.png splashscreen.png )But what CMkeLists.txt? There are several? Is it the one in the gui subdir? It also fails to say in which directory to put the pngs and where such directory should be located in the project tree. What am I missing here? I've tried to put an image folder in each subdir with all the svgs and a corresponding resources.rc file but was not able to make the project use it.
-
There is no rule where you have to put them - put them there where they are located the best. Since mainwindow is using the images i would add them there.
-
Thanks, that answers part of my question! I've already put a folder "images" inside the gui library with the used icons. But now how do I embed the resources (icons) into the library? Should I put
qt_add_resources(gui imageresources PREFIX "/images" FILES images/icon1.svg images/icon2.svg ... )in the CMakeLists.txt of subdir gui? The
PREFIX "/images"gives an error! If I remove it the build runs but I still have no icons on the app. -
Thanks, that answers part of my question! I've already put a folder "images" inside the gui library with the used icons. But now how do I embed the resources (icons) into the library? Should I put
qt_add_resources(gui imageresources PREFIX "/images" FILES images/icon1.svg images/icon2.svg ... )in the CMakeLists.txt of subdir gui? The
PREFIX "/images"gives an error! If I remove it the build runs but I still have no icons on the app.@sairun said in Problem with location of resources (svg icons) in subdir project with cmake:
e PREFIX "/images" gives an error!
And what error?
f I remove it the build runs but I still have no icons on the app.
And how do you load them?
How should we help when you don't post actual errors?
-
Without the PREFIX the build has no errors. But upon running the app I get
qt.svg: Cannot open file ':/images/document-new.svg', because: No such file or directory qt.svg: Cannot open file ':/images/document-open.svg', because: No such file or directoryand no icons appear on the menus. I'm doing this through qtcreator if that matters!
-
Sorry, in
mainwindow.cppI use_newFileAction->setIcon( QIcon( ":/images/document-new.svg" ) );to load the image.
In a previous version of this application which is structred without subdirs, I have all source files (.cpp and .h) in the same folder, together wit the CMakeLists.txt and a resources.rc file plus a folder named /images with the .svg icons. The resource.rc file has
<RCC> <qresource prefix="/"> <file>images/system-log-out.svg</file> <file>images/document-new.svg</file> <file>images/document-open.svg</file> <file>images/preferences-system.svg</file> <file>images/help.svg</file> </qresource> </RCC>and in the CMakeLists.txt I have
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) qt_add_executable(myapp MANUAL_FINALIZATION ${PROJECT_SOURCES} resources.qrc )This works properly! I suspect my problem here is not understanding cmake. There is something missing that makes the icons not being embedded into the application. But I don't now where.
-
Well, for future reference, I solved the thing by adding the images' folder to the app subdir, not to the gui subdir where these resources are mandatory.
<project root> ├── CMakeLists.txt └── src ├── app │ ├── images │ │ ├── document-new.svg │ │ └── document-open.svg │ ├── CMakeLists.txt │ └── main.cpp ├── core │ ├── CMakeLists.txt │ ├── core.cpp │ └── core.h └── gui ├── CMakeLists.txt ├── mainwindow.cpp └── mainwindow.hand editing the corresponding CMakeLists.txt file with the
qt_add_resourcesfunction! I had tried it before, but without quoting the <RESOURCE NAME> ("app_images" in my case) and that was probably the reason the PREFIX "/" was throwing an error.qt_add_executable(myapp main.cpp) qt_add_resources(myapp "app_images" PREFIX "/" FILES images/document-new.svg images/document-open.svg ) target_link_libraries(mayapp PRIVATE Qt6::Widgets core gui) set_target_properties(myapp PROPERTIES WIN32_EXECUTABLE ON MACOSX_BUNDLE ON )This way, there is no need to write a resources (.qrc) file on app subdir
-
S sairun has marked this topic as solved on