Hello! I have recently systematically studied the content related to QML modular development using CMake under Qt 6.8, and tried to write a set of configurations. To ensure that the project structure is clear, complies with modern CMake specifications, and avoids potential hidden dangers in subsequent maintenance, I hope to invite experienced friends to help review my configuration ideas and implementation details to see if there are any non-standard, optimizable, or missing parts.
Project structure:
D:\PROJECT\QT\MODERNTEST
| CMakeLists.txt
|
\---src
CMakeLists.txt
Images.qrc
main.cpp
Test.qml
Global CMakeLists.txt:
cmake_minimum_required(VERSION 3.16)
project(ModernTest VERSION 0.1 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTORCC ON)
find_package(Qt6 6.8 REQUIRED COMPONENTS Quick)
qt_standard_project_setup(REQUIRES 6.8)
add_subdirectory(src)
Sub CMakeLists.txt:
add_executable(${PROJECT_NAME}
main.cpp
)
qt_add_qml_module(${PROJECT_NAME}
URI QMLApp
VERSION 0.1
QML_FILES
Test.qml
RESOURCES
Images.qrc
)
set_target_properties(${PROJECT_NAME} PROPERTIES
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE
)
target_link_libraries(${PROJECT_NAME}
PRIVATE Qt6::Quick
)
install(TARGETS ${PROJECT_NAME}
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char* argv[]){
QGuiApplication a(argc,argv);
QQmlApplicationEngine engine;
engine.loadFromModule("QMLTest","Test");
return a.exec();
}
The above is the configuration I organized based on what I learned in class and the official manual. To ensure compliance with modern CMake specifications and avoid potential issues, I sincerely ask all experts to kindly offer their advice and check if there are any omissions or "modern" techniques that can be optimized.