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
-
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 -
L Lopher has marked this topic as solved on
-
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@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.svgThey are in :/icons/resources/images/close.svg