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. QML Repeater doesn't update when its model change in C++?

QML Repeater doesn't update when its model change in C++?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
9 Posts 3 Posters 6.1k 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.
  • AlienA Offline
    AlienA Offline
    Alien
    wrote on last edited by
    #1

    Hello,
    QML Repeater doesn't update when its model change in C++ I made two classes with names DataModel and DataProvider, DataProvider is responsible to add one name to DataModel which derived from QAbstractListModel after 2 second after adding new name QML couldn't find any changes so the list never update.
    The below link is my simple test code that doing what I explain.
    https://ufile.io/4yqbk
    This link active for 30 days

    I would appreciate any help.

    1 Reply Last reply
    0
    • AlienA Offline
      AlienA Offline
      Alien
      wrote on last edited by
      #9

      The problem is for qml side it needs to use roles that declare in model class otherwise the Qml side doesn't update at all.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        shaan7
        wrote on last edited by
        #2

        This is the problem-

        void DataModel::insertData(const QString &data)
        {
            m_listStr<<data;
            QModelIndex tl=index(m_listStr.length()-1,0);
        
            emit dataChanged(tl,tl);
        }
        

        dataChanged is for when existing row changes. Here you are adding a new row so you need to use beginInsertRows and endInsertRows instead.

        AlienA 1 Reply Last reply
        3
        • S shaan7

          This is the problem-

          void DataModel::insertData(const QString &data)
          {
              m_listStr<<data;
              QModelIndex tl=index(m_listStr.length()-1,0);
          
              emit dataChanged(tl,tl);
          }
          

          dataChanged is for when existing row changes. Here you are adding a new row so you need to use beginInsertRows and endInsertRows instead.

          AlienA Offline
          AlienA Offline
          Alien
          wrote on last edited by Alien
          #3

          Dear @shaan7,
          Thanks for you reply but may I know why dataChanged doesn't work? Adding data to a list means change the data am I right?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            shaan7
            wrote on last edited by
            #4

            From the docs -
            This signal is emitted whenever the data in an existing item changes.
            In this context, "data" really means an "existing item", not "all data" in the model. So when you add to a list, you're not modifying any existing items, you're adding new ones.

            AlienA 2 Replies Last reply
            3
            • S shaan7

              From the docs -
              This signal is emitted whenever the data in an existing item changes.
              In this context, "data" really means an "existing item", not "all data" in the model. So when you add to a list, you're not modifying any existing items, you're adding new ones.

              AlienA Offline
              AlienA Offline
              Alien
              wrote on last edited by
              #5
              This post is deleted!
              1 Reply Last reply
              0
              • S shaan7

                From the docs -
                This signal is emitted whenever the data in an existing item changes.
                In this context, "data" really means an "existing item", not "all data" in the model. So when you add to a list, you're not modifying any existing items, you're adding new ones.

                AlienA Offline
                AlienA Offline
                Alien
                wrote on last edited by
                #6

                @shaan7 said in QML Repeater doesn't update when its model change in C++?:

                In this context, "data" really means an "existing item", not "all data" in the model

                Which signal is for all data changed?

                JonBJ 1 Reply Last reply
                0
                • AlienA Alien

                  @shaan7 said in QML Repeater doesn't update when its model change in C++?:

                  In this context, "data" really means an "existing item", not "all data" in the model

                  Which signal is for all data changed?

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #7

                  @Alien
                  The docs link @shaan7 gave you shows all the signals. There are separate signals for updating data in existing rows/columns versus ones for inserting/deleting rows/columns. There is not one individual signal which covers any/all of these. Your code should raise the signal(s) correctly corresponding to whatever you are doing.

                  AlienA 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Alien
                    The docs link @shaan7 gave you shows all the signals. There are separate signals for updating data in existing rows/columns versus ones for inserting/deleting rows/columns. There is not one individual signal which covers any/all of these. Your code should raise the signal(s) correctly corresponding to whatever you are doing.

                    AlienA Offline
                    AlienA Offline
                    Alien
                    wrote on last edited by
                    #8

                    Dear @JonB,

                    Ok but why my setData function doesn't work?

                    void DataModel::changeData(int row, const QString &str)
                    {
                        if(row<0 || row>=m_listStr.length())
                            return;
                        QModelIndex modelIndex=index(row,0);
                        setData(modelIndex,QVariant::fromValue(str),Qt::DisplayRole);
                    }
                    /*********************************************************/
                    bool DataModel::setData(const QModelIndex &index, const QVariant &value, int role)
                    {
                        Q_UNUSED(role)
                        if(index.row()<0 || index.row()>=m_listStr.length())
                            return false;
                    
                        m_listStr.replace(index.row(),value.toString());
                        emit dataChanged(index,index);
                        return true;
                    }
                    
                    1 Reply Last reply
                    0
                    • AlienA Offline
                      AlienA Offline
                      Alien
                      wrote on last edited by
                      #9

                      The problem is for qml side it needs to use roles that declare in model class otherwise the Qml side doesn't update at all.

                      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