Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Qt6.8 + CMake: QML components cannot find qrc resources built by qt_add_resources
Qt 6.11 is out! See what's new in the release blog

Qt6.8 + CMake: QML components cannot find qrc resources built by qt_add_resources

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 2 Posters 698 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.
  • L Offline
    L Offline
    Lopher
    wrote on last edited by
    #1

    I used the recommended modern method to construct qrc,But the path in the QML file cannot find qrc.

    I have referred to:https://doc.qt.io/qt-6/qt-add-resources.html

    CMake:

    #imclient_resources.cmake
    function(imclient_setup_resources)
        add_library(IMClientResources STATIC)
    
        qt_add_resources(IMClientResources "IMClientSvg"
            PREFIX "/icons"
            FILES
                resources/images/close.svg
                resources/images/maximize.svg
                resources/images/minimize.svg
                resources/images/restore.svg
        )
    
        target_link_libraries(IMClientResources PRIVATE Qt6::Core)
    endfunction()
    
    #imclient_app.cmake
    function(imclient_setup_application)
        qt_add_executable(IMClientSrc
            src/main.cpp
        )
    
        target_link_libraries(IMClientSrc PRIVATE
            Qt6::Core
            Qt6::Quick
            Qt6::Qml
            Qt6::Gui
            IMClientQml
            IMClientResources
        )
    
        qt_import_qml_plugins(IMClientSrc)
    
        install(TARGETS IMClientSrc
            RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
            BUNDLE DESTINATION ${CMAKE_INSTALL_BINDIR}
        )
    endfunction()
    
    #Global CMake
    qt_standard_project_setup()
    

    TitleBar.qml

            // Minimize button
            TitleBarButton {
                width: buttonWidth
                height: buttonHeight
                visible: showMinimize
                iconSource: "qrc:/icons/minimize.svg"
                iconSize: titleBar.iconSize
                onClicked: if (targetWindow) targetWindow.showMinimized()
            }
    
            // Maximize/Restore button
            TitleBarButton {
                id: maximizeButton
                width: buttonWidth
                height: buttonHeight
                visible: showMaximize
                iconSource: targetWindow && targetWindow.visibility === Window.Maximized ?
                    "qrc:/icons/restore.svg" : "qrc:/icons/maximize.svg"
                iconSize: titleBar.iconSize
                onClicked: toggleMaximize()
            }
    
            // Close button
            TitleBarButton {
                width: buttonWidth
                height: buttonHeight
                visible: showClose
                iconSource: "qrc:/icons/close.svg"
                iconSize: titleBar.iconSize
                hoverColor: closeButtonHoverColor
                onClicked: if (targetWindow) targetWindow.close()
            }
    

    Application output:

    qrc:/qt/qml/IMClient/qml/components/TitleBarButton.qml:21:5: QML Image: Cannot open: qrc:/icons/close.svg
    qrc:/qt/qml/IMClient/qml/components/TitleBarButton.qml:21:5: QML Image: Cannot open: qrc:/icons/maximize.svg
    qrc:/qt/qml/IMClient/qml/components/TitleBarButton.qml:21:5: QML Image: Cannot open: qrc:/icons/minimize.svg
    
    Christian EhrlicherC 1 Reply Last reply
    0
    • L Lopher has marked this topic as solved on
    • L Lopher

      I used the recommended modern method to construct qrc,But the path in the QML file cannot find qrc.

      I have referred to:https://doc.qt.io/qt-6/qt-add-resources.html

      CMake:

      #imclient_resources.cmake
      function(imclient_setup_resources)
          add_library(IMClientResources STATIC)
      
          qt_add_resources(IMClientResources "IMClientSvg"
              PREFIX "/icons"
              FILES
                  resources/images/close.svg
                  resources/images/maximize.svg
                  resources/images/minimize.svg
                  resources/images/restore.svg
          )
      
          target_link_libraries(IMClientResources PRIVATE Qt6::Core)
      endfunction()
      
      #imclient_app.cmake
      function(imclient_setup_application)
          qt_add_executable(IMClientSrc
              src/main.cpp
          )
      
          target_link_libraries(IMClientSrc PRIVATE
              Qt6::Core
              Qt6::Quick
              Qt6::Qml
              Qt6::Gui
              IMClientQml
              IMClientResources
          )
      
          qt_import_qml_plugins(IMClientSrc)
      
          install(TARGETS IMClientSrc
              RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
              BUNDLE DESTINATION ${CMAKE_INSTALL_BINDIR}
          )
      endfunction()
      
      #Global CMake
      qt_standard_project_setup()
      

      TitleBar.qml

              // Minimize button
              TitleBarButton {
                  width: buttonWidth
                  height: buttonHeight
                  visible: showMinimize
                  iconSource: "qrc:/icons/minimize.svg"
                  iconSize: titleBar.iconSize
                  onClicked: if (targetWindow) targetWindow.showMinimized()
              }
      
              // Maximize/Restore button
              TitleBarButton {
                  id: maximizeButton
                  width: buttonWidth
                  height: buttonHeight
                  visible: showMaximize
                  iconSource: targetWindow && targetWindow.visibility === Window.Maximized ?
                      "qrc:/icons/restore.svg" : "qrc:/icons/maximize.svg"
                  iconSize: titleBar.iconSize
                  onClicked: toggleMaximize()
              }
      
              // Close button
              TitleBarButton {
                  width: buttonWidth
                  height: buttonHeight
                  visible: showClose
                  iconSource: "qrc:/icons/close.svg"
                  iconSize: titleBar.iconSize
                  hoverColor: closeButtonHoverColor
                  onClicked: if (targetWindow) targetWindow.close()
              }
      

      Application output:

      qrc:/qt/qml/IMClient/qml/components/TitleBarButton.qml:21:5: QML Image: Cannot open: qrc:/icons/close.svg
      qrc:/qt/qml/IMClient/qml/components/TitleBarButton.qml:21:5: QML Image: Cannot open: qrc:/icons/maximize.svg
      qrc:/qt/qml/IMClient/qml/components/TitleBarButton.qml:21:5: QML Image: Cannot open: qrc:/icons/minimize.svg
      
      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Lopher said in Qt6.8 + CMake: QML components cannot find qrc resources built by qt_add_resources:

      Cannot open: qrc:/icons/close.svg

      Because they are not there as you can see here:

          qt_add_resources(IMClientResources "IMClientSvg"
              PREFIX "/icons"
              FILES
                  resources/images/close.svg
      

      They are in :/icons/resources/images/close.svg

      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
      2

      • Login

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