Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. ListView won't get updated when I change data in my custom model
QtWS25 Last Chance

ListView won't get updated when I change data in my custom model

Scheduled Pinned Locked Moved Unsolved General and Desktop
modelslistviewlistmodeldata models
11 Posts 3 Posters 1.8k Views
  • 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.
  • I Offline
    I Offline
    ivarec
    wrote on last edited by
    #1

    I've decided to go down the road of creating a custom model for a part of my application, so that I can easily reuse it with components like ListView. I can't get it to work. The ListView won't ever get updated - it will show my model's initial data forever.

    My model inherits from QAbstractListModel. Here is a list of the methods that I've reimplemented, following the documentation's suggestions:

    • roleNames
    • rowCount
    • data
    • flags
    • insertRows
    • removeRows

    Here is a list of the signals that I emit in the appropriate places, following the documentation's suggestions:

    • dataChanged
    • beginInsertRows
    • endInsertRows

    I'm afraid that this is not enough to implement a simple custom model. What am I missing here? What is needed for a ListView to notice that my data has changed?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Are you sure you're calling dataChanged() with the correct values?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      I 1 Reply Last reply
      2
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        can you show us your code?

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        I 1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          Are you sure you're calling dataChanged() with the correct values?

          I Offline
          I Offline
          ivarec
          wrote on last edited by
          #4

          @Christian-Ehrlicher I pass two QModelIndex objects, one initiated to the top left row and column of my change and one initiated to its bottom right. Via debugger, I can see that the values make sense. For example:

          My model has 4 columns (fixed, doesn't change). If I append a new row and my model previously had 2 rows, What I see is:

          topLeft -> 2, 0
          bottomRight -> 2, 3
          
          1 Reply Last reply
          0
          • VRoninV VRonin

            can you show us your code?

            I Offline
            I Offline
            ivarec
            wrote on last edited by
            #5

            @VRonin I will setup a minimal test case

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              And is the parent correct?

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              I 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                And is the parent correct?

                I Offline
                I Offline
                ivarec
                wrote on last edited by
                #7

                @Christian-Ehrlicher my model is created once upon startup in main.cpp. I create it with my QGuiApplication as the parent. My ListView is also a child of this QGuiApplication. Is this the wrong way to do it?

                VRoninV 1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  The QModelndex you're passing to dataChanged() need a proper parent index: http://doc.qt.io/qt-5/model-view-programming.html#parents-of-items

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  0
                  • I ivarec

                    @Christian-Ehrlicher my model is created once upon startup in main.cpp. I create it with my QGuiApplication as the parent. My ListView is also a child of this QGuiApplication. Is this the wrong way to do it?

                    VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #9

                    @ivarec said in ListView won't get updated when I change data in my custom model:

                    My ListView is also a child of this QGuiApplication.

                    This is unrelated but probably bad

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    I 1 Reply Last reply
                    0
                    • VRoninV VRonin

                      @ivarec said in ListView won't get updated when I change data in my custom model:

                      My ListView is also a child of this QGuiApplication.

                      This is unrelated but probably bad

                      I Offline
                      I Offline
                      ivarec
                      wrote on last edited by
                      #10

                      @VRonin Maybe I've expressed myself badly, since I'm still a newbie in Qt. The ListView is created via QML. It's as basic as:

                      ListView {
                        id: list
                        model: myModel
                      }
                      

                      Maybe, when done like that, the parent of the ListView is not the application itself, which must be why you were surprised.

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Can we finally see where and how you emit dataChanged()? And with which indexes?

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        1

                        • Login

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