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. Automatic update of listView and c++ model
Forum Updated to NodeBB v4.3 + New Features

Automatic update of listView and c++ model

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 2 Posters 2.8k Views 1 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.
  • M Offline
    M Offline
    Merinas
    wrote on last edited by
    #1

    Hi,

    I'm trying to write a c++ model like the one describe in the example (Animal model). I would like to add a function to modified the model. I've add a Q_INVOKABLE function in the AnimalModel class. 
    

    @ void AnimalModel::changeData(int number,QString type, QString size)
    {
    if ( number >= 0 && number < m_animals.count())
    {
    qDebug() <<"number:"<<number << " new type : "<< type<<" new size : "<< size;
    m_animals[number].m_type = type;
    m_animals[number].m_size = size;
    emit dataChanged(index(0),index(m_animals.count()));
    }
    }@

    I see my message in the warning window but my view never update. I've read Model subclassing reference but I haven't found a thing to help me.

    Thanks for reading me.

    1 Reply Last reply
    0
    • L Offline
      L Offline
      ludde
      wrote on last edited by
      #2

      No idea what your AnimalModel is and where it comes from, but it looks to me like you are changing your underlying data structure (m_animals) and then tell the view that the model has changed. But you never update the model (or tell the model that the underlying data has changed).

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Merinas
        wrote on last edited by
        #3

        Animal model is the one defined "here":http://doc-snapshot.qt-project.org/4.8/declarative-modelviews-abstractitemmodel.html
        I've only change the Animal class to have all membres public.

        My model is a QList of Animal, which are modified line 6 and 7 and I guess I warn the model is updated line 8.

        1 Reply Last reply
        0
        • L Offline
          L Offline
          ludde
          wrote on last edited by
          #4

          Sorry, that was a very confusing response from me...
          Now that I see the code, I realise there's nothing wrong with your code, except for one tiny problem, and that is that when you emit dataChanged the second argument is out-of-range. It should be:
          @emit dataChanged(index(0), index(m_animals.count() - 1));@
          But that will invalidate the entire model, so really, you should just do:
          @emit dataChanged(index(number), index(number));@

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Merinas
            wrote on last edited by
            #5

            That's what I tried first but it didn't work, so I changed it just to see.

            If I did all correctly I should raise it as a bug ?

            1 Reply Last reply
            0
            • L Offline
              L Offline
              ludde
              wrote on last edited by
              #6

              Are you sure? I tried it, and it works just fine for me.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Merinas
                wrote on last edited by
                #7

                Yeah you're right that's working... I don't konw why this was'nt working the first time.
                Thanks a lot.

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  ludde
                  wrote on last edited by
                  #8

                  Great. You're welcome!

                  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