当我刚刚创建一个项目并编写cmake时,一切都很顺利,当我开始添加新的qml文件然后执行配置时,会出现2个警告:
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.
我已经关注了这个链接,但我没有找到任何对自己有用的信息(也许我看起来很糟糕),但我意识到整个要点都在 qmldir 中,我挣扎了很长时间,但没有任何结果无论如何,目前 CMake 文件和文件位置如下所示:
文件树
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}
)
将一个qml文件导入到另一个qml文件中也存在问题,在本例中为LoginWindow中的Colors,当我指定文件的路径时,项目被构建,但启动时出现以下错误:
qrc:/qt/qml/Client/src/view/loginwindow/LoginWindow.qml:3:1: "../../styles/Colors.qml": no such directory
登录窗口.qml
import QtQuick 2.15
import QtQuick.Controls
import "../../styles/Colors.qml" as Colors
ApplicationWindow {
visible: true
color: Colors.backgroundColor
}
颜色.qml
pragma Singleton
import QtQuick 2.15
QtObject {
property color backgroundColor: "#DDEFFF"
}
我也尝试使用qmldir文件和qrc,但没有任何效果,也许我做错了什么
我仍在尝试弄清楚 QML 和 CMake,我花了将近两天的时间来寻找问题,所以如果这是一个愚蠢的情况,请不要严格,我将不胜感激任何帮助
OC:Ubuntu 24.04.1 LTS
更新
我构建了Qt(calqlatr)中提供的项目,它也给出了完全相同的警告。
当我更改 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
)
对于这些:
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
)
然后这一行不再出现:
/home/zxctatar/Qt/6.8.0/gcc_64/lib/cmake/Qt6Qml/Qt6QmlMacros.cmake:1148
(qt6_add_qml_module)
我看到您使用的是 QT 6.8.0。在这种情况下添加:
qt_policy(SET QTP0004 NEW)
到您的 CMakelists.txt 文件,位于“qt_add_executable”和“qt _add_qml_module”部分。这应该可以解决警告。(https://doc.qt.io/qt-6/qt-cmake-policy-qtp0004.html)
至于Color.qml导入。您可以尝试将其添加为模块而不是相对路径。