[Solved] Updating QTableView with more rows dynamically
-
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?
-
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?
-
There're 2 things here.
-
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.
-
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.- I have a class MyModelClass that derives QAbstractTableModel class
- 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
- How can I sync the dataSource with my View?