Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with location of resources (svg icons) in subdir project with cmake
Forum Updated to NodeBB v4.3 + New Features

Problem with location of resources (svg icons) in subdir project with cmake

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 152 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sairun
    wrote on last edited by
    #1

    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.h
    

    The 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 mainwindow is 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 target helloworld one 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.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sairun
        wrote on last edited by sairun
        #3

        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.

        Christian EhrlicherC 1 Reply Last reply
        0
        • S sairun

          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.

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sairun
            wrote on last edited by
            #5

            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 directory
            

            and no icons appear on the menus. I'm doing this through qtcreator if that matters!

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sairun
              wrote on last edited by sairun
              #6

              Sorry, in mainwindow.cpp I 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.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                sairun
                wrote on last edited by
                #7

                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.h
                

                and editing the corresponding CMakeLists.txt file with the qt_add_resources function! 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

                1 Reply Last reply
                0
                • S sairun has marked this topic as solved on

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved