Updating QML view from C++

  • Quick question.
    Suppose that I have view in QML displaying some list model defined on the C++ side. In the model I provide the roles (e.g. "power" and "active"). That way in QML I can refer to them via their names and I do some bindings in delegate (e.g. some CheckBox's "checked" to "active" and some Text's "text" to "power").

    Everything works OK when it comes to displaying (view is requesting data for correct roles and so on).

    When I update model on the C++ side I wanted to mark "dirty" only "power" or only "active", so that QML requests only data for the changed part. However it seems that when emitting dataChanged() I have to use Qt::DisplayRole (which requests data for all roles - above I mentioned only "power" and "active" but there are more defined by me). If I emit dataChanged() with my defined roles (enum with PowerRole = Qt::UserRole + 1,...) then view is not updating information.

    Is there a way to notify QML view that something has changed in the model but in a way that it will request only particular (user defined) role?

    Best regards

  • Qt Champions 2017

    I have done this & it only updates the requested role. Can you copy your code here ? I will check if any issues.

  • @AndrzejO Quick counter-question: did you implement roleNames()?

  • Thank you @dheerendra and @Eeli-K for taking look at it (and sorry for not replying earlier). When I was pasting code here to show you how I do it I actually has found the problem :) - my apologies for the noise.

    It was how I created vector of roles in dataChanged() (I have not used braces) and was emitting it like:

        emit dataChanged(createIndex(0,0), createIndex(2,0), QVector<int>(PowerRole));

    instead of:

        emit dataChanged(createIndex(0,0), createIndex(2,0), QVector<int>({PowerRole}))

    I wonder how it ever worked when I used QVector<int>(Qt::DisplayRole) - probably by accident :)

    Thank you for your time
    Best regards

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.