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.