Send a signal to a QML form embedded in a QQuickWidget
-
I have a QML form that I display inside of a QQuickWidget in a C++ class. I want to be able to send a signal to the QML form from the class that the QQuickWidget is created in.
eg.
Class MainWindow
{
...
qmlwidget = new QQuickWidget()
qmlwidget->setSource(QUrl::fromLocalFile(filepath)
}Is there a way to reference the QML form so I can connect a signal to it? If not a signal, is there a way to trigger the functions of the QML form from the parent C++ class?
-
Hi,
Did you already take a look at the QtQml C++ Integration chapter in Qt's documentation ?
-
I have a QML form that I display inside of a QQuickWidget in a C++ class. I want to be able to send a signal to the QML form from the class that the QQuickWidget is created in.
eg.
Class MainWindow
{
...
qmlwidget = new QQuickWidget()
qmlwidget->setSource(QUrl::fromLocalFile(filepath)
}Is there a way to reference the QML form so I can connect a signal to it? If not a signal, is there a way to trigger the functions of the QML form from the parent C++ class?
@EqiupmentConnected get QQuickItem * quick_item = QQuickWidget->rootObject();
if you want to change a value defined in qml, use the following:
https://doc.qt.io/qt-5/qobject.html#setProperty -
@EqiupmentConnected get QQuickItem * quick_item = QQuickWidget->rootObject();
if you want to change a value defined in qml, use the following:
https://doc.qt.io/qt-5/qobject.html#setProperty@JoeCFD no don't do that, that's bad practice.
https://doc.qt.io/qt-6/qtquick-bestpractices.html#separate-ui-from-business-logic
https://doc.qt.io/qt-6/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c
https://youtu.be/vzs5VPTf4QQ?t=23m20sInstead expose an object as a Singleton to QML and use the Connections type to handle its signal.
-
@JoeCFD no don't do that, that's bad practice.
https://doc.qt.io/qt-6/qtquick-bestpractices.html#separate-ui-from-business-logic
https://doc.qt.io/qt-6/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c
https://youtu.be/vzs5VPTf4QQ?t=23m20sInstead expose an object as a Singleton to QML and use the Connections type to handle its signal.
-
Got it to work using:
QObject *rootObject = qmlwidget->rootObject()
QMetaObject::invokeMethod(rootObject, "functionName")https://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html#invoking-qml-methods
Thanks for the help.
-
E EqiupmentConnected has marked this topic as solved on
-
Got it to work using:
QObject *rootObject = qmlwidget->rootObject()
QMetaObject::invokeMethod(rootObject, "functionName")https://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html#invoking-qml-methods
Thanks for the help.
😬😬😬😬😬
-
@GrecKo I guess I understood what your idea is. It is similar like creating a model for listview. Change the data in the model to update QML code instead of settting the properties into QML code directly.
An example:
https://scythe-studio.com/en/blog/how-to-integrate-c-and-qml-registering-c-class-as-singleton-to-qml