Qt 6.5: accessing QML file from C++
-
wrote on 6 Apr 2023, 16:43 last edited by
Hi all -
I'm in the process of converting my application to 6.5, and taking advantage of the new QML module features as described here. As part of the conversion, I stopped using a qml.qrc file. My files are all defined in my CMakeLists.txt file:
qt_add_qml_module(appnga_demo URI nga_demo VERSION 1.0 QML_FILES Main.qml Activitydrawer.qml Auxtabbar.qml ...
I can access all of my QML objects from my qml files by importing nga_demo. But my C++ code (of course) doesn't "see" my qml files:
QUrl("qrc:/Pump.qml") // <== obviously doesn't work
So, do I need my qml.qrc file after all, or is there a new way to reference the files in my module?
Thanks...
-
wrote on 9 Apr 2023, 05:35 last edited by
From a high level, the module API looks like a nice thing to use. When I've directly instantiated QML from C++, it's only ever been to load a single top level Item. As @mzimmers suggests, I've found it better to keep the presentation layer pretty much to itself. The interaction that is necessary should be via signals and slots, or events and event handlers.
A C++ slot might be the right place to instantiate an instance of a QQuickComponent provided by a QML caller. This is how containers such as Repeater and ListModel work.
-
wrote on 8 Apr 2023, 08:23 last edited by jeremy_k 4 Aug 2023, 08:24
I haven't used the facility yet, but this appears to be answered in the same document:
The line of interest is const QUrl url(u"qrc:/qt/qml/helloqml/main.qml"_s);. There are multiple gotchas in here. The first is that because we are dealing with a QML module, main.qml has now been placed under a helloqml folder[...]
-
I haven't used the facility yet, but this appears to be answered in the same document:
The line of interest is const QUrl url(u"qrc:/qt/qml/helloqml/main.qml"_s);. There are multiple gotchas in here. The first is that because we are dealing with a QML module, main.qml has now been placed under a helloqml folder[...]
wrote on 8 Apr 2023, 12:42 last edited by@jeremy_k I guess what I'm wondering is whether the recommended procedure for exposing QML files to C++ is still the qml.qrc file, or if there's a newer, better way to do it.
After thinking about this problem, it occurs to me that in this particular example, I probably shouldn't be referencing the file in C++ anyway, as this information is better kept in the QML side of the house, but I'm still interested in the preferred way to go about it.
-
@jeremy_k I guess what I'm wondering is whether the recommended procedure for exposing QML files to C++ is still the qml.qrc file, or if there's a newer, better way to do it.
After thinking about this problem, it occurs to me that in this particular example, I probably shouldn't be referencing the file in C++ anyway, as this information is better kept in the QML side of the house, but I'm still interested in the preferred way to go about it.
wrote on 8 Apr 2023, 13:59 last edited by@mzimmers
There is access via URI and typename e.g. in https://doc.qt.io/qt-6/qqmlcomponent.html#QQmlComponent-6
In general, I recommend reading this: https://www.qt.io/blog/whats-new-for-qml-modules-in-6.5 -
wrote on 9 Apr 2023, 05:35 last edited by
From a high level, the module API looks like a nice thing to use. When I've directly instantiated QML from C++, it's only ever been to load a single top level Item. As @mzimmers suggests, I've found it better to keep the presentation layer pretty much to itself. The interaction that is necessary should be via signals and slots, or events and event handlers.
A C++ slot might be the right place to instantiate an instance of a QQuickComponent provided by a QML caller. This is how containers such as Repeater and ListModel work.
-
1/5