-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
register QML types at compile time #241
Comments
This is a prerequisite for ahead-of-time compilation of QML: #242 |
I think we should even register the type as a qml type by default? E.g.: |
Ah, also we should probably have options for QML_IMPORT_NAME/QML_IMPORT_MAJOR_VERSION in our cmake 🤔 |
Qt doesn't automatically register all QObjects as QML types by default, so I don't think we should. |
I think we should follow Qt as this then links to doing singletons which would then look like this (from #25 )
|
Looks like this would be set by
However i think this was only added 6.x series, so how would you set this in 5.15 ? https://doc.qt.io/qt-6/qt-add-qml-module.html#qt6-add-qml-module |
Hm, maybe that's not possible in Qt 5 with CMake at all? We could only enable the feature if Qt6 is used... |
@ahayzen-kdab I am working on this issue, can you assign it to me? :) |
It's not possible to register QML types at build time with Qt5 and CMake, however, it is possible with qmake, meaning all the tooling exists in both Qt5 and Qt6, just not the CMake wrapper in Qt5. Qt6QmlMacros.cmake (/usr/lib64/cmake/Qt6Qml/Qt6QmlMacros.cmake from Fedora's qt6-qtdeclarative-devel package) has a function _qt_internal_qml_type_registration which runs an undocumented CLI tool called qmltyperegistrar which generates some C++ code. We could run that from qt-build-utils & cxx-qt-build, which AFAIK should work with both Qt5.15 and Qt6 and also work for Cargo-only builds without CMake. It seems qmltyperegistar needs a JSON file that moc outputs, but I'll let @vimpostor figure out those details. |
Hi I have the proof of concept working now (Qt6 only for now), but there are 2 major issues I encountered that probably require some API redesign:
@Be-ing I think adding Qt5 cmake support for this would be out of scope for a PR for the base implementation as it is already getting complicated enough, but it surely can be added later. |
@vimpostor Thanks for the detailed information! So I think that for 1), @Be-ing was talking about looking at what CMake does, then adding support into our For 2) we currently by default always link to Core and Gui, so guess we could add Qml too. We were also thinking in #290 to add features into cxx-qt-lib for the different modules so you could have something that just uses Core. But this would be in the -build and -gen phases. I wonder if we can also have a feature and/or ifdef so that if Qml support is enabled in cxx-qt/cxx-qt-build/cxx-qt-gen it causes the QML_ELEMENT to occur. This feature would be enabled by default but eg if you were working on a widgets or QtCore only projefct you could disable this feature ? tl;dr; yes enabling Qml module linking by default makes sense, but would be good if we can disable with a feature or similar somewhere. |
Oof, yeah, it seems the only reasonable way to implement this robustly is to not use |
Initially I was thinking a new method would need to be added to CxxQtBuilder to specify the information that is passed to |
So this could be quite repetitive for multiple objects with the same uri and version. Maybe we need a fallback global uri / version that can be set with CxxQtBuilder, then a local override in the macro ?
So there is also |
@Be-ing is looking into the CMake / qt-build-utils side further :-) |
I've been investigating how the qt_add_qml_module CMake function works and it's quite involved (and the internals aren't documented much):
This is quite a lot to avoid 2 lines of boilerplate C++ code. 🙃 |
Yes, I noticed that too :D But very impressive, if you are able to get it working. |
by specifying [cxxqt::qobject(qml_uri = "foo.bar", qml_verison = "1.0")] Fixes KDAB#241
by specifying [cxxqt::qobject(qml_uri = "foo.bar", qml_verison = "1.0")] Fixes KDAB#241
by specifying [cxxqt::qobject(qml_uri = "foo.bar", qml_verison = "1.0")] Fixes KDAB#241
by specifying [cxxqt::qobject(qml_uri = "foo.bar", qml_verison = "1.0")] Fixes KDAB#241
by specifying [cxxqt::qobject(qml_uri = "foo.bar", qml_verison = "1.0")] Fixes KDAB#241
by specifying [cxxqt::qobject(qml_uri = "foo.bar", qml_verison = "1.0")] Fixes KDAB#241
by specifying [cxxqt::qobject(qml_uri = "foo.bar", qml_verison = "1.0")] Fixes KDAB#241
by specifying [cxxqt::qobject(qml_uri = "foo.bar", qml_verison = "1.0")] Fixes KDAB#241
by specifying [cxxqt::qobject(qml_uri = "foo.bar", qml_verison = "1.0")] Fixes KDAB#241
by specifying [cxxqt::qobject(qml_uri = "foo.bar", qml_verison = "1.0")] Fixes KDAB#241
by specifying [cxxqt::qobject(qml_uri = "foo.bar", qml_verison = "1.0")] Fixes KDAB#241
by specifying [cxxqt::qobject(qml_uri = "foo.bar", qml_verison = "1.0")] Fixes KDAB#241
by specifying [cxxqt::qobject(qml_uri = "foo.bar", qml_verison = "1.0")] Fixes KDAB#241
by specifying [cxxqt::qobject(qml_uri = "foo.bar", qml_verison = "1.0")] Fixes KDAB#241
by specifying [cxxqt::qobject(qml_uri = "foo.bar", qml_verison = "1.0")] Fixes KDAB#241
by specifying [cxxqt::qobject(qml_uri = "foo.bar", qml_verison = "1.0")] Fixes KDAB#241
by specifying [cxxqt::qobject(qml_uri = "foo.bar", qml_verison = "1.0")] Fixes KDAB#241
by specifying [cxxqt::qobject(qml_uri = "foo.bar", qml_verison = "1.0")] Fixes KDAB#241
by specifying [cxxqt::qobject(qml_uri = "foo.bar", qml_verison = "1.0")] Fixes KDAB#241
This feature was introduced in Qt 5.15 and allows for getting rid of the
qmlRegisterType
boilerplate in C++:https://www.qt.io/blog/qml-type-registration-in-qt-5.15
https://doc-snapshots.qt.io/qt5-5.15/qtqml-cppintegration-definetypes.html
I suggest an API something like:
If an application wants to support Qt < 5.15, I think they could simply omit the
qml_element
from the attribute and callqmlRegisterType
in their C++.Removing the requirement of calling
qmlRegisterType
would make it feasible to launch a QML application from Rust with bindings to QGuiApplication and QQmlApplication without needing to write any C++.The text was updated successfully, but these errors were encountered: