Important: Please read the Qt Code of Conduct -

How to make qml aware of changes happened in C++ side?

  • Hey,
    Imagine you have a Qvector of Qstring type and you put some names there something like :

    Qvector<QString> names;

    In qml you implement a repeater to show these names inside of some rectangles.What if you change one name of this vector from c++ side?How you can force repeater to repeat again and update the qml side to show new names?

    How many ways are exist for that and which one has better performance?

  • Moderators

    I guess you are after a property binding.

    Thus you should define a custom type with a QStringList property. Make sure that the property has a NOTIFY signal and trigger this signal whenever the property changes.

    For example:

    Q_PROPERTY( QStringList myList READ getMyList WRITE setMyList NOTIFY myListChanged )

    Then trigger the myListChanged signal in the setMyList setter method.

  • Dear @raven-worx ,
    That's true one solution is using model of repeater like QStringList, QAbstractItemModel,etc but is there any other solution for that something that cause repaint in qml item therefor repeater force to repeat again and finally shows updated data on the screen?

  • Moderators

    why would it just need to be repainted?
    If the data has changed you should notify it as i said. Or in case of a QAbstractItemModel emit the dataChanged() signal

  • @raven-worx ,
    Thanks for your solution it seems that there is no need to repaint,Thank you.

Log in to reply