PDA

View Full Version : QTableModel - beginInsertRows, index not known



tuli
19th August 2015, 22:33
Hi,


I have a model that I derived from a QAbstractTableModel. It provides access to a datasource.

When I modify a "row" equivalent of the datasource, I only know the index of that "row" until *after* the modification is done. It would be almost as expensive as the actual modification to find out the index of the "row" that is to be modified (and currently the datasource doesnt support that). the problem is of course that I need the row index for the beginInsertRows function.


I tried it, and it seems to work just fine to first modify the datasource and then call beginInsertRow + endInsertRow. The alternative is to do beginResetModel/endResetModel.


Do I have to expect any negative side effects? Would you recommend to just reset the model?


The same problem exists with beginRemoveRows.

anda_skoa
20th August 2015, 10:23
When you modify a row, wouldn't that just require dataChanged()?

Anyway, if you don't see any bad behavior in your view you should be fine.

And insert/remove is definitely better than reset.

Cheers,
_

tuli
20th August 2015, 14:27
Yes, I meant insert a row or delete a row. But great, I'll just leave it like that then.


Btw, another minor issue: when I emit dataChanged signal, and the model is set to a tablewidget that does not have focus right now, then the data will not be updated in the UI. It will only be updated when I bring the tablewidget to the foreground/focus. Can I change it so it updates right away?

anda_skoa
20th August 2015, 15:24
That is strange, I would have assumed that any cell that is visible will get updated if that cell is part of the range of the dataChanged() signal.

What do you mean with "foreground/focus" or rather what is the state when it is not that?

Cheers,
_

tuli
22nd August 2015, 08:00
Imagine I have two show()n widgets: one tableview with a model and a separate edit widget, that edits data in the tablview's model and emits dataChanged(). When I have the edit widget on focus, the tableview goes out of focus. Any edit now of a in the background visible talbeview cell will not update the cell. Instead I have to click on the tableview to bring it into focus, and then the cell will update.


(On windows there is always only one focused/foreground window. Other windows might be visible on the screen, but do not have focus.)

anda_skoa
22nd August 2015, 11:30
So the edit widget calls setData() on the model, the model emits dataChanged(), but the view does not update?

Cheers,
_

tuli
22nd August 2015, 13:16
Got it! While writing a short example i found the mistake:

I was passing in index(idx, columnCount()), but that's an invalid index. Should be index(idx, columnCount() - 1). And then it works. :)