c++qtcmakeqml

I can't import one qml file into another, the same reflection in CMake when setting up a Qt QML project


When I just create a project and write cmake, everything goes well, when I start adding new qml files and then perform the configuration, then 2 warnings come out:

 CMake Warning (dev) at /home/zxctatar/Qt/6.8.0/gcc_64/lib/cmake/Qt6Core/Qt6CoreMacros.cmake:3378 (message):
  Qt policy QTP0004 is not set: You need qmldir files for each extra
  directory that contains .qml files for your module.  Check
  https://doc.qt.io/qt-6/qt-cmake-policy-qtp0004.html for policy details.
  Use the qt_policy command to set the policy and suppress this warning.

Call Stack (most recent call first):   
/home/zxctatar/Qt/6.8.0/gcc_64/lib/cmake/Qt6Qml/Qt6QmlMacros.cmake:3238 
(__qt_internal_setup_policy)   
/home/zxctatar/Qt/6.8.0/gcc_64/lib/cmake/Qt6Qml/Qt6QmlMacros.cmake:891 
(qt6_target_qml_sources)   
/home/zxctatar/Qt/6.8.0/gcc_64/lib/cmake/Qt6Qml/Qt6QmlMacros.cmake:1148 
(qt6_add_qml_module)   CMakeLists.txt:15 (qt_add_qml_module) This 
warning is for project developers.  Use -Wno-dev to suppress it.

I already followed this link, but I didn’t find any useful information for myself (maybe I was looking poorly), but I realized that the whole point is in qmldir, I struggled with it for a long time, but nothing came of it anyway, at the moment the CMake file and The file locations look like this:

File tree

Client/
├──build\
├──src\
│   ├──styles\
│   │   └──Colors.qml
│   ├──view\
│   │   ├──loginwindow\
│   │   │   └──LoginWindow.qml
│   ├──main.cpp
├──CMakeLists.txt
└──CMakeLists.txt.user

CMakeLists.txt

cmake_minimum_required(VERSION 3.16)

project(Client VERSION 0.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt6 6.5 REQUIRED COMPONENTS Quick)

qt_standard_project_setup(REQUIRES 6.5)

qt_add_executable(appClient
    src/main.cpp
)

qt_add_qml_module(appClient
    URI Client
    VERSION 1.0
    QML_FILES

        QML_FILES src/view/loginwindow/LoginWindow.qml
        QML_FILES src/styles/Colors.qml
)

# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
set_target_properties(appClient PROPERTIES
#    MACOSX_BUNDLE_GUI_IDENTIFIER com.example.appClient
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

target_link_libraries(appClient
    PRIVATE Qt6::Quick
)

include(GNUInstallDirs)
install(TARGETS appClient
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

There is also a problem with importing one qml file into another, in this case Colors in LoginWindow, when I specify the path to the file, the project is built, but when launched it gives the following error:

qrc:/qt/qml/Client/src/view/loginwindow/LoginWindow.qml:3:1: "../../styles/Colors.qml": no such directory

LoginWindow.qml

import QtQuick 2.15
import QtQuick.Controls
import "../../styles/Colors.qml" as Colors

ApplicationWindow {
    visible: true

    color: Colors.backgroundColor
}

Colors.qml

pragma Singleton
import QtQuick 2.15

QtObject {
    property color backgroundColor: "#DDEFFF"
}

I also tried to use qmldir files and qrc, but nothing worked, maybe I did something wrong

I’m still trying to figure out QML and CMake, I spent almost two days searching for the problem, so if this is a stupid situation, don’t be strict, I’ll be grateful for any help

OC: Ubuntu 24.04.1 LTS


UPDATE

I built the project provided in Qt (calqlatr), it also gives exactly the same warnings.

Also when I changed these lines in CMakeLists.txt:

qt_standard_project_setup(REQUIRES 6.5)
qt_add_executable(appClient
    src/main.cpp
)
qt_add_qml_module(appClient
    URI Client
    VERSION 1.0
    QML_FILES

        QML_FILES src/view/loginwindow/LoginWindow.qml
        QML_FILES src/styles/Colors.qml
)

for these:

qt6_standard_project_setup(REQUIRES 6.5)
qt6_add_executable(appClient
    src/main.cpp
)
qt6_add_qml_module(appClient
    URI Client
    VERSION 1.0
    QML_FILES

        QML_FILES src/view/loginwindow/LoginWindow.qml
        QML_FILES src/styles/Colors.qml
)

then this line no longer appears:

/home/zxctatar/Qt/6.8.0/gcc_64/lib/cmake/Qt6Qml/Qt6QmlMacros.cmake:1148 
    (qt6_add_qml_module)

Solution

  • I see you are using QT 6.8.0. In that case add:

    qt_policy(SET QTP0004 NEW)
    

    to your CMakelists.txt file, inbetween "qt_add_executable" and "qt _add_qml_module" sections. That should solve the warnings. (https://doc.qt.io/qt-6/qt-cmake-policy-qtp0004.html)

    As for the Color.qml import. You might try adding it as a module instead of a relative path.