Qt new CMake API + qmllint + qt_add_qml_module - how to setup
-
So I switched my project to Qt 6.8
qt_add_qml_module()
API, and wanted to try out theqmllint
andqmlformat
. I have dummy basic project:C:. CMakeLists.txt main.cpp Main.qml MyButton.qml
The
CMakeLists.txt
:cmake_minimum_required(VERSION 3.20) project(qml-sample VERSION 0.1 LANGUAGES CXX) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(Qt6 6.8 REQUIRED COMPONENTS Quick) qt_standard_project_setup(REQUIRES 6.8) qt_add_executable(qml-sample main.cpp) qt_add_qml_module(qml-sample URI AppMain VERSION 1.0 QML_FILES Main.qml MyButton.qml ) target_link_libraries(qml-sample PRIVATE Qt6::Quick)
The
main.cpp
:#include <QGuiApplication> #include <QQmlApplicationEngine> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; QObject::connect( &engine, &QQmlApplicationEngine::objectCreationFailed, &app, []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.loadFromModule("AppMain", "Main"); return app.exec(); }
The
Main.qml
:import QtQuick 2.15 import QtQuick.Controls 2.15 Window { width: 640 height: 480 visible: true title: qsTr("Hello World") MyButton { anchors.centerIn: parent width: 100 height: 50 } }
Add dummy control
MyButton.qml
:import QtQuick 2.15 import QtQuick.Controls 2.15 import QtQuick.Layouts 2.15 Rectangle { Button { text: "Click me!" anchors.fill: parent } }
And that works fine, when I call qmllint target
qml-sample_qmllint
by hand:Info: C:/sandbox/qml-test/Main.qml:2:1: Unused import [unused-imports] import QtQuick.Controls 2.15 ^^^^^^ Info: C:/sandbox/qml-test/MyButton.qml:3:1: Unused import [unused-imports] import QtQuick.Layouts 2.15 ^^^^^^
and in QtCreator I can see that issues are reported too:
Now I wanted to modularize things, and made two separate QML modules - one with Main.qml the
AppMain
and one with my controlsControls
, and link them to main application, so now structure looks like:C:. │ CMakeLists.txt │ main.cpp │ Main.qml │ └───controls CMakeLists.txt MyButton.qml
So
controls/CMakeLists.txt
:qt_add_qml_module(controls STATIC NO_PLUGIN URI Controls VERSION 1.0 QML_FILES MyButton.qml OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/Controls )
And main
CMakeLists.txt
changes:--- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,13 +6,15 @@ find_package(Qt6 6.8 REQUIRED COMPONENTS Quick) qt_standard_project_setup(REQUIRES 6.8) +add_subdirectory(controls) + qt_add_executable(qml-sample main.cpp) -qt_add_qml_module(qml-sample +qt_add_qml_module(app-main STATIC NO_PLUGIN URI AppMain VERSION 1.0 QML_FILES Main.qml - MyButton.qml + OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/AppMain ) -target_link_libraries(qml-sample PRIVATE Qt6::Quick) +target_link_libraries(qml-sample PRIVATE Qt6::Quick app-main controls)
And project still compiles and works fine, but
qmllint
is not working - i get many errors when calling qmllint targets forapp-main
:Warning: C:/sandbox/qml-test/Main.qml:10:5: MyButton was not found. Did you add all imports and dependencies? [import] MyButton { ^^^^^^^^ Info: Did you mean "Button"? Button { ^^^^^^ Warning: C:/sandbox/qml-test/Main.qml:11:9: unknown grouped property scope anchors. [unqualified] anchors.centerIn: parent ^^^^^^^ Warning: C:/sandbox/qml-test/Main.qml:10:5: 'MyButton' is used but it is not resolved [unresolved-type] MyButton { ^^^^^^^^ Warning: C:/sandbox/qml-test/Main.qml:11:9: 'anchors' is used but it is not resolved [unresolved-type] anchors.centerIn: parent ^^^^^^^ ...
QtCreator is also confused:
What I'm missing or not doing? Any help appreciated :)