Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Updating QML view from C++
Forum Updated to NodeBB v4.3 + New Features

Updating QML view from C++

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 998 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AndrzejO
    wrote on last edited by
    #1

    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
    Andrzej

    E 1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

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

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • A AndrzejO

        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
        Andrzej

        E Offline
        E Offline
        Eeli K
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • A Offline
          A Offline
          AndrzejO
          wrote on last edited by
          #4

          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
          Andrzej

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved