[Solved] Updating QTableView with more rows dynamically



  • 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.



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



  • 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



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



  • 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.



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



  • 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.



  • 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();@



  • 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?



  • 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() ?



  • 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..



  • 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.



  • 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 :(



  • [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?



  • 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.



  • 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.



  • 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.



  • 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?



  • 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.



  • OK.

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

    Thanks a lot for your help. Really appreciate it.



  • 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?

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.