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.
  • S Offline
    S Offline
    starbearer
    wrote on 15 Feb 2013, 04:03 last edited by
    #1

    Hello,

    I have a QTableView, which uses a model derived from QABstractTableModel.

    The model starts out with a number of rows and displays the data properly.

    The model also has a timer attached to it, which upon expiring, gets the number of rows and columns, constructs an index for it, and emits dataChanged signal, to update the dynamic contents of the table.

    The problem is..when the number of rows it is supposed to display, changes.

    In that case, even though I obtain a new index with the changed no. of rows, and update the table, it still doesn't display any new rows.

    I've identified why, perhaps. Let's say I have 2 rows in the beginning, and next, 5 rows are supposed to be displayed. In the timer expiration logic, I've asked it to ocnstruct a new index, with new row count...but dataChanged() signal, will only change the data for the rows which had already been inserted in the first. The new rows are not displayed.

    This is my code for the model.
    @TableLayout::TableLayout()
    : QAbstractTableModel()
    {
    rowCount();
    columnCount();

    timer = new QTimer(this);
    timer->setInterval(3000);
    timer->start();
    connect(timer, SIGNAL(timeout()) , this, SLOT(timerHit()));

    //ItemList[0].SetFields("Blah" ,"Blah" , "Blah" , "Blah" , "Blah", "Blah", true );
    //ItemList[1].SetFields("Blah1" ,"Blah1" ,"Blah1" ,"Blah1" ,"Blah1", "Blah1", true );
    }

    void TableLayout::timerHit()
    {
    int row = this->rowCount();
    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);
    data(Ind1);
    emit dataChanged(Ind, Ind1);
    }

    int TableLayout::rowCount(const QModelIndex & /parent/) const
    {
    RtdbReader *rtdbReader = RtdbReader::instance();
    if (isConnected)
    return rtdbReader->Get_Number_of_Items();
    else return 2;
    }

    int TableLayout::columnCount(const QModelIndex & /parent/) const
    {

    return 6;
    }

    QVariant TableLayout::data(const QModelIndex &index, int role) const
    {
    int row = index.row();
    int col = index.column();

    //return row;
    int row_count , col_count = 0;
    if ( role == Qt::DisplayRole) {

     if( col == 1)
    

    return ItemList[row]->GetExplanation();
    if ( col == 2)
    {qDebug()<<" Now printing Sig name for row"<< index.row();
    return ItemList[row]->GetCond_Sig_Name();
    }
    if ( col == 3)
    return ItemList[row]->GetDescription();
    if ( col == 5)
    return ItemList[row]->GetLogic();
    if ( col == 4)
    return ItemList[row]->Get_State_Text();
    }
    else if ( role == Qt::DecorationRole && col == 0 )
    {
    bool col_to_display = ItemList[row]->GetValueColour();
    qDebug()<<"colour in view:"<<col_to_display;
    if ( col_to_display )
    {
    QString path = "Green.png";
    //QPixmap p( s_type1Icon );
    // return s_type1Icon;

    QIcon icon ( path );
    return icon;
    

    }
    else
    {
    QString path = "Yellow.png";
    //QPixmap p( s_type1Icon );
    // return s_type1Icon;

    QIcon icon ( path );
    return icon;
    

    }
    }

    if (role == Qt::TextAlignmentRole )
    return Qt::AlignCenter | Qt::AlignVCenter;

    /* else if ( role == Qt::BackgroundRole)
    {
    QBrush yellowBackground(Qt::yellow);
    return yellowBackground;
    }*/

    return QVariant();
    }@

    Please suggest how I can change the view so new rows are added.

    Thanks.

    1 Reply Last reply
    1
    • Q Offline
      Q Offline
      qxoz
      wrote on 15 Feb 2013, 04:17 last edited by
      #2

      Put your row adding action between
      @beginInsertRows ( const QModelIndex & parent, int first, int last )
      ...
      endInsertRows()@

      1 Reply Last reply
      1
      • J Offline
        J Offline
        Jeroentjehome
        wrote on 15 Feb 2013, 08:02 last edited by
        #3

        emit dataChanged(Ind, Ind1);
        Is almost the same as reset(). Reset on the other hand will also reset any proxy model selection stuff.
        Just curious, but why don't you use ItemList as a QList? Deriving a QList is tricky though, so be carefull to do so. Then in rowCount etc you simple return the number of items in QList.count().
        then as qxoz replied use the beginInsert/EndInsert functions. After the endInsertRows the View will update the changed cells automatic.
        Greetz

        Greetz, Jeroen

        1 Reply Last reply
        1
        • Q Offline
          Q Offline
          qxoz
          wrote on 15 Feb 2013, 08:14 last edited by
          #4

          One more thing. If you want remove some rows then add some new better use:
          @beginResetModel();
          ...
          endResetModel();@

          1 Reply Last reply
          1
          • A Offline
            A Offline
            andre
            wrote on 15 Feb 2013, 08:38 last edited by
            #5

            No, you don't. It is better to really indicate which rows you are going to remove, and which ones you are adding instead of doing a model reset. If possible at all, avoid model resets. They are very annoying for the user, as they cause large visual disruptions and loss of selections.

            1 Reply Last reply
            1
            • Q Offline
              Q Offline
              qxoz
              wrote on 15 Feb 2013, 08:51 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 15 Feb 2013, 08:53 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 15 Feb 2013, 08:58 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 15 Feb 2013, 10:23 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 15 Feb 2013, 11:17 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 15 Feb 2013, 13:10 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 15 Feb 2013, 13:24 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 15 Feb 2013, 13:32 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 15 Feb 2013, 13:32 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 15 Feb 2013, 13:35 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 16 Feb 2013, 05:14 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 16 Feb 2013, 11:36 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 16 Feb 2013, 11:44 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 16 Feb 2013, 11:49 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 16 Feb 2013, 12:13 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

                                          1/22

                                          15 Feb 2013, 04:03

                                          • Login

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