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
-
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(); }
Hi @StudentScripter, yes in general your structure is fine.
I just noticed something strange::
RESOURCE_PREFIX / # wichtig: sonst wird es zur Laufzeit nicht gefunden ... mymodules # <-- statische lib (ohne "plugin")
I would remove
RESOURCE_PREFIX /
and changemymodules
tomymodulesplugin
. Does it still work for you? -
Hi @StudentScripter, yes in general your structure is fine.
I just noticed something strange::
RESOURCE_PREFIX / # wichtig: sonst wird es zur Laufzeit nicht gefunden ... mymodules # <-- statische lib (ohne "plugin")
I would remove
RESOURCE_PREFIX /
and changemymodules
tomymodulesplugin
. Does it still work for you?@JKSH removing RESOURCE_PREFIX / is no problem, works still fine, thanks. :D
However do you mean changing all mymodules mentions to mymodulesplugin or only this one? Cause when changing only one it gives me errors.
Here are all my files:
CMakeLists.txt inside mymodules subfolder:
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 instead of SHARED/PLUGIN qt_add_qml_module(mymodules URI mymodules # entspricht: import mymodules 1.0 VERSION 1.0 QML_FILES Buttons.qml Qmld.qml XP3DDebugViewer.qml SOURCES Myhelper.hpp Myhelper.cpp ) set_target_properties(mymodules PROPERTIES QT_QML_MODULE TRUE ) 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} )
In Main.qml i import it as:
import mymodules 1.0 Qmld { id: krass anchors.fill: parent } Buttons { id: neubtn anchors.bottom: parent.bottom anchors.right: parent.right }
-
Also another question what to do if i wanted to add another subfolder inside my
mymodules/
directory? Lets say:mymodules/extras
-
@JKSH removing RESOURCE_PREFIX / is no problem, works still fine, thanks. :D
However do you mean changing all mymodules mentions to mymodulesplugin or only this one? Cause when changing only one it gives me errors.
Here are all my files:
CMakeLists.txt inside mymodules subfolder:
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 instead of SHARED/PLUGIN qt_add_qml_module(mymodules URI mymodules # entspricht: import mymodules 1.0 VERSION 1.0 QML_FILES Buttons.qml Qmld.qml XP3DDebugViewer.qml SOURCES Myhelper.hpp Myhelper.cpp ) set_target_properties(mymodules PROPERTIES QT_QML_MODULE TRUE ) 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} )
In Main.qml i import it as:
import mymodules 1.0 Qmld { id: krass anchors.fill: parent } Buttons { id: neubtn anchors.bottom: parent.bottom anchors.right: parent.right }
@StudentScripter said in Feedback needed: QML is this the right way to link other QML Files?:
removing RESOURCE_PREFIX / is no problem, works still fine, thanks. :D
Good to hear, that's what I expected :) There were some changes in Qt 6.5 to make things easier, so you should no longer need to specify
RESOURCE_PREFIX
unless you want some unconventional structure (which is not recommended)However do you mean changing all mymodules mentions to mymodulesplugin or only this one?
I meant only one:
target_link_libraries(appQmlLibraryTest PRIVATE Qt6::Quick mymodulesplugin # <-- Like this )
Cause when changing only one it gives me errors.
Can you please share the full error message? Without it, it makes it hard to troubleshoot for you.
@StudentScripter said in Feedback needed: QML is this the right way to link other QML Files?:
Also another question what to do if i wanted to add another subfolder inside my
mymodules/
directory? Lets say:mymodules/extras
You could create a new QML module (in
mymodules/extras/CMakeLists.txt
)