Newbie - shared object between C++ and QML
-
Hi all,
I'm trying to share a C++ object with QML code.
I defined a IoBoard class and instantiated.
Then shared it with QML cause I need to recall some functions.
In my debug prints I see that the engine created two objects !IoBoard ioboard; ioboard.setType((IoBoard::CONNECTION_SERIAL)); qmlRegisterType<IoBoard>("com.my.locker", 1, 0, "IoBoard");
How can i have a single object?
Thanks!
-
Do you have a call anywhere in your application to "QQmlContext::setContextProperty"?
https://doc.qt.io/qt-5/qqmlcontext.html#setContextProperty
If not, then that could be the one piece you are missing.
I have a template project (that is built and tested regularly in GitHub CI to prove its viability) that contains an end-to-end working example:
- Here are the calls to
setContextProperty
: https://github.com/219-design/qt-qml-project-template-with-ci/search?q=setcontextproperty&unscoped_q=setcontextproperty - Here is where the QML code then uses
customLoggingCategories
(one of the C++ objects exported to QML): https://github.com/219-design/qt-qml-project-template-with-ci/blob/c61bbd4fbfb4aeb4f9c96b5baedeb935e3064ae9/src/lib_app/qml/LogTags.qml - Here is where the QML code uses
resourceHelper
(another object exported to QML): https://github.com/219-design/qt-qml-project-template-with-ci/blob/79e71e40be0874dd9fcc229b9685a4371697d3cf/src/lib_app/qml/main.qml
You can also download a source distribution of the Qt5 sources and look at their examples, which are often excellent.
You might not be particularly interested in Video Effects, but this official Qt example uses
setContextProperty
and exposes several C++ objects to QML: https://doc.qt.io/qt-5/qtmultimedia-multimedia-video-qmlvideofx-example.htmlSo does this one: https://doc.qt.io/qt-5.9/qtquickcontrols2-gallery-example.html
Code:
- Here are the calls to
-
Hi,
How are you using it in your QML code ?
-
Do you have a call anywhere in your application to "QQmlContext::setContextProperty"?
https://doc.qt.io/qt-5/qqmlcontext.html#setContextProperty
If not, then that could be the one piece you are missing.
I have a template project (that is built and tested regularly in GitHub CI to prove its viability) that contains an end-to-end working example:
- Here are the calls to
setContextProperty
: https://github.com/219-design/qt-qml-project-template-with-ci/search?q=setcontextproperty&unscoped_q=setcontextproperty - Here is where the QML code then uses
customLoggingCategories
(one of the C++ objects exported to QML): https://github.com/219-design/qt-qml-project-template-with-ci/blob/c61bbd4fbfb4aeb4f9c96b5baedeb935e3064ae9/src/lib_app/qml/LogTags.qml - Here is where the QML code uses
resourceHelper
(another object exported to QML): https://github.com/219-design/qt-qml-project-template-with-ci/blob/79e71e40be0874dd9fcc229b9685a4371697d3cf/src/lib_app/qml/main.qml
You can also download a source distribution of the Qt5 sources and look at their examples, which are often excellent.
You might not be particularly interested in Video Effects, but this official Qt example uses
setContextProperty
and exposes several C++ objects to QML: https://doc.qt.io/qt-5/qtmultimedia-multimedia-video-qmlvideofx-example.htmlSo does this one: https://doc.qt.io/qt-5.9/qtquickcontrols2-gallery-example.html
Code:
- Here are the calls to
-
@SteMMo said in Newbie - shared object between C++ and QML:
In this way:
IoBoard { id: ioBoard } {... onClicked: ioBoard.openLock(2) ...}
And here you are: you instanciate it once in your main function and you create another one here.
Either remove the one from you main or remove this one and use a context property as suggested by @KH-219Design.
-
Is very well described here: https://qml.guide/singletons/ and here https://doc.qt.io/qt-5/qqmlengine.html#qmlRegisterSingletonType. Is good to know which Qt Version you are using, because registration has been changed in Qt 5.15.0. Unfortunately I cannot find the drawing in the Qt Documentation, where was recommended which solution you should use qmlRegisterType / setcontextProperrty etc. in some use cases.