Feedback needed: QML is this the right way to link other QML Files?
-
So my goal was to seperate my .qml files from the Main.qml by creating a subfolder called mymodules/ which contains my other .qml files as a library. But im not sure if my CMakeLists.txt are the most optimal. Can anyone check and give me feeback? <3 Thanks
My structure looks like this:
mymodules CMakeLists.txt:
cmake_minimum_required(VERSION 3.16) project(mymodules VERSION 1.0 LANGUAGES CXX) find_package(Qt6 REQUIRED COMPONENTS Quick) qt_add_library(mymodules STATIC) # <--- STATIC statt SHARED/PLUGIN qt_add_qml_module(mymodules URI mymodules # entspricht: import mymodules 1.0 VERSION 1.0 QML_FILES Buttons.qml RESOURCE_PREFIX / # wichtig: sonst wird es zur Laufzeit nicht gefunden ) target_link_libraries(mymodules PRIVATE Qt6::Quick )
Main CMakeLists.txt:
cmake_minimum_required(VERSION 3.16) project(QmlLibraryTest VERSION 0.1 LANGUAGES CXX) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) find_package(Qt6 REQUIRED COMPONENTS Quick) qt_standard_project_setup(REQUIRES 6.8) qt_add_executable(appQmlLibraryTest main.cpp ) qt_add_qml_module(appQmlLibraryTest URI QmlLibraryTest VERSION 1.0 QML_FILES Main.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(appQmlLibraryTest PROPERTIES # MACOSX_BUNDLE_GUI_IDENTIFIER com.example.appQmlLibraryTest MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} MACOSX_BUNDLE TRUE WIN32_EXECUTABLE TRUE ) # Baue und installiere das Sub‑Modul mit add_subdirectory(mymodules) target_link_libraries(appQmlLibraryTest PRIVATE Qt6::Quick mymodules # <-- statische lib (ohne "plugin") ) include(GNUInstallDirs) install(TARGETS appQmlLibraryTest 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 app(argc, argv); QQmlApplicationEngine engine; QObject::connect &engine, &QQmlApplicationEngine::objectCreationFailed, &app, []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.loadFromModule("QmlLibraryTest", "Main"); return app.exec(); }
-
So my goal was to seperate my .qml files from the Main.qml by creating a subfolder called mymodules/ which contains my other .qml files as a library. But im not sure if my CMakeLists.txt are the most optimal. Can anyone check and give me feeback? <3 Thanks
My structure looks like this:
mymodules CMakeLists.txt:
cmake_minimum_required(VERSION 3.16) project(mymodules VERSION 1.0 LANGUAGES CXX) find_package(Qt6 REQUIRED COMPONENTS Quick) qt_add_library(mymodules STATIC) # <--- STATIC statt SHARED/PLUGIN qt_add_qml_module(mymodules URI mymodules # entspricht: import mymodules 1.0 VERSION 1.0 QML_FILES Buttons.qml RESOURCE_PREFIX / # wichtig: sonst wird es zur Laufzeit nicht gefunden ) target_link_libraries(mymodules PRIVATE Qt6::Quick )
Main CMakeLists.txt:
cmake_minimum_required(VERSION 3.16) project(QmlLibraryTest VERSION 0.1 LANGUAGES CXX) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) find_package(Qt6 REQUIRED COMPONENTS Quick) qt_standard_project_setup(REQUIRES 6.8) qt_add_executable(appQmlLibraryTest main.cpp ) qt_add_qml_module(appQmlLibraryTest URI QmlLibraryTest VERSION 1.0 QML_FILES Main.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(appQmlLibraryTest PROPERTIES # MACOSX_BUNDLE_GUI_IDENTIFIER com.example.appQmlLibraryTest MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} MACOSX_BUNDLE TRUE WIN32_EXECUTABLE TRUE ) # Baue und installiere das Sub‑Modul mit add_subdirectory(mymodules) target_link_libraries(appQmlLibraryTest PRIVATE Qt6::Quick mymodules # <-- statische lib (ohne "plugin") ) include(GNUInstallDirs) install(TARGETS appQmlLibraryTest 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 app(argc, argv); QQmlApplicationEngine engine; QObject::connect &engine, &QQmlApplicationEngine::objectCreationFailed, &app, []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.loadFromModule("QmlLibraryTest", "Main"); return app.exec(); }
why you tagged me, I'm not an expert in QML?
But I would put all qml files in the qt resource system. -
why you tagged me, I'm not an expert in QML?
But I would put all qml files in the qt resource system.@Pl45m4 Thought you'd may have some experience regarding the cmake aspect. :D But nevermind.
-
Hello, i'm trying to do something similar, but only with qt_add_qml_module, check out this:
https://github.com/Deymoss/MoneyTogether/tree/master/modules/UI
and this
https://forum.qt.io/topic/162027/correct-way-to-make-qml-modules-qt-6.5-6.9/14
So far, I have not come to a solution to the problem, but I think I will solve it in the next week, thanks a lot to Mike Trahearn, he pushed this question in the qt expert community and said that he has an example that is currently still in progress, so we need to wait and there will be an answer. -
Hello, i'm trying to do something similar, but only with qt_add_qml_module, check out this:
https://github.com/Deymoss/MoneyTogether/tree/master/modules/UI
and this
https://forum.qt.io/topic/162027/correct-way-to-make-qml-modules-qt-6.5-6.9/14
So far, I have not come to a solution to the problem, but I think I will solve it in the next week, thanks a lot to Mike Trahearn, he pushed this question in the qt expert community and said that he has an example that is currently still in progress, so we need to wait and there will be an answer.@Deymos Well gonna have a look at that, but nevertheless my approach posted above does work without error utilizing qt_add_qml_module but im not sure if its the best approach or has any potential errors. Thats why i was asking.
-
@Deymos Well gonna have a look at that, but nevertheless my approach posted above does work without error utilizing qt_add_qml_module but im not sure if its the best approach or has any potential errors. Thats why i was asking.
@StudentScripter
Yes, this approach that you provided will work without problems, but I want to try to do it withoutqt_add_library(mymodules STATIC)
so that there is not a lot of junk in the project tree, and it turns out to be possible. But we have to wait