Nominate our 2022 Qt Champions!

Force refresh of model data in QML

  • I have some QML. Which to a large extent I do not understand. Part of it shows a Grid, and that Gird has an underlying model back in the main QT code. Sometimes, the data that comes back from that model changes. The model itself has not changed; only that some of the data (in this particular case, an icon) will be different when asked for.

    How can I make the shown icon refresh? I was thinking I could create a timer like this:

        Timer {
            interval: 1000;
            repeat: true;
            running: true;
            onTriggered: { refresh (); }

    but I don't know how that refresh() function could work. How can I enforce a reloading of the data from the model? I though I could perhaps "change" the model the grid is using, but change it to the same one again - presumably that would cause the grid to refetch all the data, but I don't know how to change the model the grid is using.

    The model is put into one of these Grid objects like this:

        Grid {
            id: availableCompGrid
            header: qsTr("Components")
            model: availableCompModel
            anchors.left: parent.left
            anchors.right: parent.right
            onClicked: componentHelpRequested(item.component)

  • What kind of model are you using?

  • availableCompModel is a QSortFilterProxyModel, with source model for that being a QAbstractItemModel.

    The specific actual model in question behind it all is a subclass of a QAbstractTableModel, which is of course itself a subclass of a QAbstractItemModel.

  • If I'm reading your original post correctly: I reckon't it suffice to explicitly emit the QAbstractItemModel::dataChanged signal from C++ when you change the icon source, assuming you're changing the icon source yourself.

  • I do not change the icon source myself. Its state is effectively indeterminate until fetched. I could have the C++ emit dataChanged every ten seconds just in case it has changed, but it seems clumsy to have the C++ emitting signals purely to make the QML refresh every ten seconds (and that would also cause other parts of the code that don't care about the icon changes to go refetching all the data as well).

Log in to reply