Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QML and C++ Integration



  • Hi,

    I am creating a desktop based application in QML. This application dynamically updates the device status(like ON/OFF ) on the UI. The status is received in the C++ class file, the same status needs to be updated in QML.
    I used Signal Slot mechanism for Integration with C++ and QML.
    The problem encountered is : Only when the signal is emitted from QML the C++ slot is executed in and following the signal emitted from within the C++ slot the QML slot gets executed.

    Is there a mechanism that the QML slot is executed whenever a signal is generated at C++ without any QML signal being generated.

    Or is there any mechanism to update the QML dynamically without any events generated from QML.



  • To avoid the loop, you may need to provide some flag argument to your signal: if emitted from QML/C++, you set the flag to true and based on the flag, choose to emit a signal or not in the slot.
    But to be more effective, you should post some code.



  • @Diracsbracket
    Thanks for the reply.
    This solution will not fulfil my requirement.
    My requirement is
    I want to change the colour of a circle from red to green or vice versa in QML as and when the status of a device is received from the middleware. This should happen dynamically and without any user intervention. The middleware is integrated with Qt C++.
    What is the best way this can be implemented?
    Kindly help



  • @vidyabhushan said in QML and C++ Integration:

    This solution will not fulfil my requirement

    I see... My bad!


  • Qt Champions 2018

    Only when the signal is emitted from QML the C++ slot is executed in and following the signal emitted from within the C++ slot the QML slot gets executed.

    You have something weird going on, there should be no problem receiving a c++ signal from QML, regardless if a QML signal was emitted before.

    I want to change the colour of a circle from red to green or vice versa in QML as and when the status of a device is received from the middleware. This should happen dynamically and without any user intervention. The middleware is integrated with Qt C++.

    For this usecase, I would recommand going with a Q_PROPERTY(bool deviceIsOn READ deviceIsOn NOTIFY deviceIsOnChanged).
    When the state of the device is changed, emit the notify signal so that the QML side knows it has changed, and it will then call the getter to query the state.

    Thanks to the magic of QML property binding, your QML code will simply become:
    Rectangle { color: cppObject.deviceIsOn ? "green" : "red" }

    More info about exposing c++ properties here : http://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html#exposing-properties


Log in to reply