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. QTableView Not Updating After Adding a New Row
Forum Updated to NodeBB v4.3 + New Features

QTableView Not Updating After Adding a New Row

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 3 Posters 4.0k 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.
  • MasterBLBM MasterBLB

    void QStandardItemModel::appendRow(const QList<QStandardItem *> &items) is your friend Brother:

    for (int i = 0; i < clusterNames.length(); i++)
    {
        model->appendRow( { new QStandardItem(clusterNames[i]), new QStandardItem(clusterIPs[i]) } );
    }
    
    S Offline
    S Offline
    Satish Joshi
    wrote on last edited by
    #5

    @MasterBLB

    Thank You so much for your time brother.

    But sadly it didn't work. Actually, I want the view to update automatically after adding the new row through a dialog box. As I can see the changes if I reopen the particular window again.

    Also, my model has the updated data but somehow I don't know why table view is not updating.

    Thanks

    raven-worxR 1 Reply Last reply
    0
    • MasterBLBM Offline
      MasterBLBM Offline
      MasterBLB
      wrote on last edited by MasterBLB
      #6

      Hmmm...try emit dataChanged(model->index(i, 0), model->index(i, 1), <maybe you'll have to pass 3rd parameter as well>) just after the line which adds new row.
      If that won't help then there are last resort methods - beginResetModel() before adding rows, and after completing add row operation endResetModel()
      Soo, merging my answers so far the output code would be:

      for (int i = 0; i < clusterNames.length(); i++)
      {
          model->appendRow( { new QStandardItem(clusterNames[i]), new QStandardItem(clusterIPs[i]) } );
          emit dataChanged(model->index(i, 0), model->index(i, 1));//maybe you'll have to pass 3rd parameter as well
      }
      
      S 1 Reply Last reply
      0
      • S Satish Joshi

        @MasterBLB

        Thank You so much for your time brother.

        But sadly it didn't work. Actually, I want the view to update automatically after adding the new row through a dialog box. As I can see the changes if I reopen the particular window again.

        Also, my model has the updated data but somehow I don't know why table view is not updating.

        Thanks

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #7

        @Satish-Joshi said in QTableView Not Updating After Adding a New Row:

        Actually, I want the view to update automatically after adding the new row through a dialog box.

        i explained why it doesnt update.
        What exactly have you tried?!

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        S 1 Reply Last reply
        0
        • raven-worxR raven-worx

          @Satish-Joshi said in QTableView Not Updating After Adding a New Row:

          Actually, I want the view to update automatically after adding the new row through a dialog box.

          i explained why it doesnt update.
          What exactly have you tried?!

          S Offline
          S Offline
          Satish Joshi
          wrote on last edited by
          #8

          @raven-worx

          Yes I understand what you trying to explain and have used your suggested approach and
          I have used QStandardItemModel::insertRow(). But it didn't work.

          Thanks

          1 Reply Last reply
          0
          • MasterBLBM MasterBLB

            Hmmm...try emit dataChanged(model->index(i, 0), model->index(i, 1), <maybe you'll have to pass 3rd parameter as well>) just after the line which adds new row.
            If that won't help then there are last resort methods - beginResetModel() before adding rows, and after completing add row operation endResetModel()
            Soo, merging my answers so far the output code would be:

            for (int i = 0; i < clusterNames.length(); i++)
            {
                model->appendRow( { new QStandardItem(clusterNames[i]), new QStandardItem(clusterIPs[i]) } );
                emit dataChanged(model->index(i, 0), model->index(i, 1));//maybe you'll have to pass 3rd parameter as well
            }
            
            S Offline
            S Offline
            Satish Joshi
            wrote on last edited by
            #9

            @MasterBLB

            you mean to say QAbstractItemModel::dataChanged(.....) right?

            I did it like this way :

            // In header File
            signals:
                void dataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight);
            
            // On the Loop
            emit model->dataChanged(model->index(i, 0), model->index(i, 1));
            

            Sorry but this time it also didn't work or I am missing something. Please let me know

            Thanks

            raven-worxR 1 Reply Last reply
            0
            • S Satish Joshi

              @MasterBLB

              you mean to say QAbstractItemModel::dataChanged(.....) right?

              I did it like this way :

              // In header File
              signals:
                  void dataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight);
              
              // On the Loop
              emit model->dataChanged(model->index(i, 0), model->index(i, 1));
              

              Sorry but this time it also didn't work or I am missing something. Please let me know

              Thanks

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by raven-worx
              #10

              @Satish-Joshi
              you are not doing what i suggested.
              Try this:

              model = new QStandardItemModel(this);
              model->setColumnCount( 2 );
              //...
              for(int i = 0; i < clusterNames.length(); i++)
              {
                      QStandardItem *clusterName_i = new QStandardItem(clusterNames[i]);
                      QStandardItem *clusterIP_i = new QStandardItem(clusterIPs[i]);
                      model->appendRow( QList<QStandardItem*>() << clusterName_i << clusterIP_i );
              }
              

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              S 1 Reply Last reply
              0
              • raven-worxR raven-worx

                @Satish-Joshi
                you are not doing what i suggested.
                Try this:

                model = new QStandardItemModel(this);
                model->setColumnCount( 2 );
                //...
                for(int i = 0; i < clusterNames.length(); i++)
                {
                        QStandardItem *clusterName_i = new QStandardItem(clusterNames[i]);
                        QStandardItem *clusterIP_i = new QStandardItem(clusterIPs[i]);
                        model->appendRow( QList<QStandardItem*>() << clusterName_i << clusterIP_i );
                }
                
                S Offline
                S Offline
                Satish Joshi
                wrote on last edited by Satish Joshi
                #11

                @raven-worx

                Sorry. It didn't work though

                I think there might be some issue with Widget or QSubWindow because as I already mentioned that the values are already updating on the model (checked through debugging it) but couldn't be displayed on the View.

                As if I reopen the window it displays properly.

                What do you think?

                Thanks Anyway

                1 Reply Last reply
                0
                • MasterBLBM Offline
                  MasterBLBM Offline
                  MasterBLB
                  wrote on last edited by MasterBLB
                  #12

                  Use that beginResetModel() and endResetModel() methods then, these makes a model completely invalidated, and therefore force a thorough update.

                  model->beginResetModel();
                  for (int i = 0; i < clusterNames.length(); i++)
                  {
                      model->appendRow( { new QStandardItem(clusterNames[i]), new QStandardItem(clusterIPs[i]) } );  
                  }
                  model->endResetModel();
                  
                  S 1 Reply Last reply
                  0
                  • MasterBLBM MasterBLB

                    Use that beginResetModel() and endResetModel() methods then, these makes a model completely invalidated, and therefore force a thorough update.

                    model->beginResetModel();
                    for (int i = 0; i < clusterNames.length(); i++)
                    {
                        model->appendRow( { new QStandardItem(clusterNames[i]), new QStandardItem(clusterIPs[i]) } );  
                    }
                    model->endResetModel();
                    
                    S Offline
                    S Offline
                    Satish Joshi
                    wrote on last edited by
                    #13

                    @MasterBLB

                    It seems both are protected members so can't access through it.

                    Thanks

                    1 Reply Last reply
                    0
                    • MasterBLBM Offline
                      MasterBLBM Offline
                      MasterBLB
                      wrote on last edited by
                      #14

                      Inherit QStandardItemModel, and declare public functions like beginReset() and endReset() whose internally invoke these protected methods.

                      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