[SOLVED] Define Object Types using QML from C++?
-
How can I register/define an object type from a string of QML in C++ not from JavaScript?
-
Hi John,
Can you make your question more clear?
-
I have a QML document as a string in C++. I'd like to make this QML document available to other QML documents being drawn to the display in a similar fashion to the Button examples in the documentation here http://qt-project.org/doc/qt-5/qtqml-documents-definetypes.html.
Instead of a SquareButton.qml I have the QML markup stored in a string.
Edit: My current thinking is to use rcc at runtime to generate a .rcc file that can be loaded with QResource at runtime.
-
In QML you can use "Qt.createQmlObject":http://qt-project.org/doc/qt-5/qtqml-javascript-dynamicobjectcreation.html#creating-an-object-from-a-string-of-qml
if you want to use c++ there might be a c++ counterpart of that function?
maybe you can simply use "QJSEngine::evaluate":http://qt-project.org/doc/qt-5/qjsengine.html#evaluate or check the code what Qt.createQmlObject does under the hood. A simple solution you can just send the string to QML and use Qt.createQmlObject, but that might not be the best way if you already have the string in c++ :D -
@
QQmlEngine engine;
QQmlComponent component(&engine);
component.setData("import QtQuick 2.0\nText { text: "Hello world!" }", QUrl());
QQuickItem *item = qobject_cast<QQuickItem *>(component.create());//add item to view, etc
...
@