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. [Solved] Updating QTableView with more rows dynamically
Forum Updated to NodeBB v4.3 + New Features

[Solved] Updating QTableView with more rows dynamically

Scheduled Pinned Locked Moved General and Desktop
22 Posts 6 Posters 22.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.
  • Q Offline
    Q Offline
    qxoz
    wrote on last edited by
    #6

    Ok. What about cases when you need remove all old data and add new ones?

    1 Reply Last reply
    1
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #7

      Sure, if you need to completely rebuild your data, then a reset makes sense. But not if you only need to replace 10 rows out of 100 or more.

      1 Reply Last reply
      1
      • Q Offline
        Q Offline
        qxoz
        wrote on last edited by
        #8

        Yes i agree with you :)
        to starbearer: please read my older post as:
        One more thing. If you want remove all rows then add some new better use:
        @beginResetModel();
        ...
        endResetModel();@

        1 Reply Last reply
        1
        • S Offline
          S Offline
          starbearer
          wrote on last edited by
          #9

          That's exactly what I wish to do.

          All rows must be removed and the view should have new rows and data.

          So, it'd be better to reset and then set the model again?

          Where should I do this?

          In the timerHit() slot?

          1 Reply Last reply
          1
          • Q Offline
            Q Offline
            qxoz
            wrote on last edited by
            #10

            In your case yes.
            @beginResetModel();
            //remove all rows
            //add new rows
            endResetModel();@

            But i don't understand something. Can you tell me why do you call data(Ind1) (row 26) in timerHit() ?

            1 Reply Last reply
            1
            • S Offline
              S Offline
              starbearer
              wrote on last edited by
              #11

              Um..
              Why do I have to remove and add rows, between stating and resetting the model?

              Wouldn't a model reset automatically get the new number of rows and build the model thus?

              And Data(Ind1) was just something I'd been trying out, thinking it'd force the new rows to come up..

              1 Reply Last reply
              1
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #12

                This is how the model/view is designed to work: you advertise that you're going to do something (add or remove rows or columns or a reset), change the actual data, and then advertise that you're done doing it.

                You need to inform any connected view that your data structure has changed. At the same time, the QAbstractItemModel knows nothing of your actual data, so it can't automatically do it.

                1 Reply Last reply
                1
                • Q Offline
                  Q Offline
                  qxoz
                  wrote on last edited by
                  #13

                  I have very poor knowledge about deep qt architecture and ofcourse it will works in that way:
                  @//remove all rows
                  //add new rows
                  beginResetModel();
                  endResetModel();@

                  But i am pretty sure there are reasons for remove and add rows, between starting and resetting the model. Maybe somebody with deep knowledge write the reasons :)

                  edit
                  ahh, thank you Andre! I wrote this post too slowly :(

                  1 Reply Last reply
                  1
                  • S Offline
                    S Offline
                    starbearer
                    wrote on last edited by
                    #14

                    [quote author="Andre" date="1360934653"]This is how the model/view is designed to work: you advertise that you're going to do something (add or remove rows or columns or a reset), change the actual data, and then advertise that you're done doing it.

                    You need to inform any connected view that your data structure has changed. At the same time, the QAbstractItemModel knows nothing of your actual data, so it can't automatically do it.[/quote]

                    OK..
                    @
                    void TableLayout::timerHit()
                    {
                    int temp_row = this->rowCount();
                    if (row != temp_row) //row is the rowCount() from before, old row number
                    {
                    beginResetModel();
                    endResetModel();
                    }
                    qDebug()<<"RowCOunt::"<<row;
                    qDebug()<<" Now IT IS : "<<row;
                    int col = this->columnCount();
                    qDebug()<<"ColumnCOunt::"<<col;
                    QModelIndex Ind = createIndex( 0 , 0);
                    QModelIndex Ind1 = createIndex( row, col);
                    // emit rowsInserted(Ind1 , 0 , row);
                    // data(Ind1);
                    emit dataChanged(Ind, Ind1);

                    }
                    @

                    This seems to work just fine..I'm not adding any rows or deleting any..
                    When I get the information that the number of rows has changed, I merely reset the model and so on.
                    It did the trick..

                    Should I be wary of something that might happen because of this?

                    1 Reply Last reply
                    1
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #15

                      This is weird code. Why do you say you are resetting the model when you actually don't do any resetting between your calls? If you are doing a model reset, emitting the dataChanged signal is also nonsense.

                      1 Reply Last reply
                      1
                      • S Offline
                        S Offline
                        starbearer
                        wrote on last edited by
                        #16

                        There're 2 things here.

                        1. When the table is on view..it has 2 columns which show dynamic content and which need to be changed according to inputs, hence the dataChanged() signal, when the timer is hit, and new data is polled for.

                        2. When some input is changed, the entire view needs to be changed, more( or less) rows are added, with entirely new content.

                        The second was not happening because dataChanged() signal would only change the data ( according to documentation ) if any had existed in the rows in the first place. Since, more rows could now be there, but which had not been there before..dataChanged() did not work.

                        So...when I detect, in the timerHit() slot, that the number of rows is now different than the previous, I simply reset the model. It would then build a new one and add the required rows and stuff..

                        And I'm not doing reset in all cases, only when the number of rows is changed.

                        dataChanged() is essential on timerHit() slot as within the same view, content of the rows might change.

                        1 Reply Last reply
                        1
                        • A Offline
                          A Offline
                          andre
                          wrote on last edited by
                          #17

                          The point is, you are doing it wrong. The actual changing of the data needs the happen between the calls to beginResetModel and endResetModel. You are not doing that. You are first resetting the data behind the models back (bad!), then you may call the resetModel pair, and then you emit a dataChanged for the whole model. That's not good.

                          You can not change the data underlying the model if that affects the layout of the model before a call to one of the begin* methods to announce that you are going to change it. The only changes you can do without announcing beforehand, are those that don't affect the data structure, only the contents. For those, emitting dataChanged() is enough.

                          1 Reply Last reply
                          1
                          • S Offline
                            S Offline
                            starbearer
                            wrote on last edited by
                            #18

                            Alright.

                            So..
                            @ if ( row number has changed ) //which'll tell me if the model has to be changed
                            then
                            beginResetModel()
                            change the model
                            endResetModel()
                            else
                            continue along the way, including dataChanged()
                            @

                            Is this fine? This is what was implied, right?

                            1 Reply Last reply
                            1
                            • A Offline
                              A Offline
                              andre
                              wrote on last edited by
                              #19

                              No, because you use the models row number for your check. But that row number is part of the definition of the data layout of the model, and thus is not allowed to change before your call to beginResetModel.

                              Because all your data seems to change every time (if I understand you correctly), I would always use a model reset and don't bother with the dataChanged.

                              1 Reply Last reply
                              1
                              • S Offline
                                S Offline
                                starbearer
                                wrote on last edited by
                                #20

                                OK.

                                Got it. I'll try and implement it properly.

                                Thanks a lot for your help. Really appreciate it.

                                1 Reply Last reply
                                1
                                • S Offline
                                  S Offline
                                  sami1592
                                  wrote on last edited by sami1592
                                  #21

                                  Hi,
                                  my case is similar to OP but with one difference.

                                  1. I have a class MyModelClass that derives QAbstractTableModel class
                                  2. MyModelClass does not contain the dataSource(in my case this dataSource is a list of user defined class) for the view. MyModelClass only has a reference to it. This dataSource is created, modified, item added, item deleted etc from a separate class(let's say name of the class is dataSourceHolderClass). in short dataSource for the MyModelClass is not internal
                                  3. How can I sync the dataSource with my View?
                                  P 1 Reply Last reply
                                  1
                                  • S sami1592

                                    Hi,
                                    my case is similar to OP but with one difference.

                                    1. I have a class MyModelClass that derives QAbstractTableModel class
                                    2. MyModelClass does not contain the dataSource(in my case this dataSource is a list of user defined class) for the view. MyModelClass only has a reference to it. This dataSource is created, modified, item added, item deleted etc from a separate class(let's say name of the class is dataSourceHolderClass). in short dataSource for the MyModelClass is not internal
                                    3. How can I sync the dataSource with my View?
                                    P Offline
                                    P Offline
                                    Patchouli
                                    wrote on last edited by
                                    #22

                                    @sami1592 Did you know how to do it? I'm having the same problem.

                                    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