Solved Property Binding not updating after signal emitted
-
@J-Hilk I have added a guard in the
setTextBox1()
function but it is still not working. I am wondering that the initialization value ofm_textBox1
is shown properly when starting the application but updating when clicking a button is not working.Yet, I did not debug the binding loop.
-
@beckseba can you make a minimal example for us to check out?
the only thing missing from your opening post seems to be the main.cpp
-
@J-Hilk I have only registered the Messaging class
qmlRegisterType<Messaging>("hmi.messaging",1,0,"Messaging");
in the main.cpp file.In my application I have several classes which are able to update the property bindings very well. The only difference is that I call
void Messaging::setMessageText(QString message)
from annother class to trigger the update of the property. -
@beckseba sounds like you're updating a different instance of the class then the qml side has access to
-
@J-Hilk That would be a valid reason why it is not working. Have you an idea for triggering the update from annother class?
-
@beckseba from c++ side ?
afaik, there's only setting a c++ class instance as contextProperty (
setContextProperty
) orqmlRegisterSingletonType
and accessing the class instance via the getters respectivelyhttps://doc.qt.io/qt-5/qqmlcontext.html#setContextProperty
https://doc.qt.io/qt-5/qqmlengine.html#qmlRegisterSingletonType -
@J-Hilk Thank you for the hint. However, I do not understand how to update a property if the update does not take place in the main function, because I have no access to the
QQuickView view
object. -
Are there any additional ideas how to access from annother instance property bindings, or in general how to access qml properties (from c++ side) from annother class. More precisely (considering the example in the first comment), how can I update
m_textBox1
from a second class e.g.handler.cpp
so that thetextBox1
in test.qml is also updated properly?handler.cpp Messaging messagingObject; messagingObject.setMessageText("This is a new message");
Thank you for the support.
-
@beckseba said in Property Binding not updating after signal emitted:
@J-Hilk Thank you for the hint. However, I do not understand how to update a property if the update does not take place in the main function, because I have no access to the QQuickView viewobject.
Well, if you don't want to connect this inside main, and don't want to path a reference/pointer around either, then I would go with the singleton.
btw, this is the only case where I would ever suggest a singleton :P
-
@J-Hilk Thank you, the singleton approach works very well.
I have created two functions
*Messagging::instance()
and*Messaging::qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine)
and registered the second one as Singleton in my main file:Messaging *Messaging::instance() { if (m_pThis == nullptr) { m_pThis = new Messaging; } return m_pThis; } QObject *Messaging::qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine) { Q_UNUSED(engine); Q_UNUSED(scriptEngine); return Messaging::instance(); }
Now, the same instance can be used in all other classes to edit variables and update GUI elements accordingly with e.g.
Messagging *messageService = Messagging::instance(); messageService->setMessageText("xyz");