How to update QTableView when rows are inserted to it
-
@Ratzz
Hello,
I believe you should emit the QAbstractItemModel::rowsInserted signal from the model (after your call toendInsertRows
), so the view knows to adapt to the changes.Kind regards.
PS:
switch(column) { case 0:
You should really use (possibly unnamed) enums for that kind of switch constructs.
-
@kshegunov
It asks for QPrivateSignal http://postimg.org/image/bjpuc4bep/ -
@Ratzz
Oops, sorry. The note above the one you cited states:It can only be emitted by the QAbstractItemModel implementation, and cannot be explicitly emitted in subclass code.
Hm, in that case I'm not sure ... The documentation is somewhat cryptic whether
endInsertRows()
actually emits the aforementioned signal ... -
@Ratzz
Possibly, you could try implementing QAbstractItemModel::dropMimeData without intercepting the drop event? -
@kshegunov
ThebeginInsertRows
doc http://doc.qt.io/qt-4.8/qabstractitemmodel.html#beginInsertRows saysNote: This function emits the rowsAboutToBeInserted() signal which connected views (or proxies) must handle before the data is inserted. Otherwise, the views may end up in an invalid state.
Will it help?
-
@Ratzz
Yes, I saw that, but this is to notify the view insertions will be happening. The question is whetherendInsertRows()
emitsrowsInserted()
thus telling the view the insertion has finished and it can actually update itself. Unfortunately, the documentation doesn't state anything specific aboutendInsertRows()
, unless I completely missed it ...PS. I just noticed something in your code:
QTreeView * view= dynamic_cast<QTreeView*>(event->source());
Is this a typo?
-
Hi,
The documentation doesn't specify but the implementation does emit rowsInserted. However in this case insertRows doesn't do anything so I wonder if there isn't a caching mechanism that avoids to update the view if in fact nothing has changed.
-
However in this case insertRows doesn't do anything so I wonder if there isn't a caching mechanism that avoids to update the view if in fact nothing has changed.
After a quick look (without pretense to be complete) I couldn't clearly see any such caching done.
Here's what I peeked at (for Qt 5.6):
http://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/itemmodels/qabstractitemmodel.cpp#n2601
http://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/itemmodels/qabstractitemmodel.cpp#n582I drag the items from another QTreeView .The cast is for that..
Did you mean that?Indeed I did. Have you considered handling the drag/drop events through the ordinary mechanism? Serializing on drag, then deserializing the mime data on drop?
-
@kshegunov : I mean't view side caching.
@Ratzz currently you are not inserting anything through it. Like @kshegunov suggested, implement the drag and drop through the mechanism provided for model/view classes. It's described here
-
No i have not considered this part.
Then I suggest you try it out. It may prove to be easier and better behaved than pulling the records directly from the tree widget. As for your current implementation, I'm at a loss why it doesn't work. Maybe the tree view is losing the selection when dragging and the selection model returns no records?
I mean't view side caching.
I see, well I didn't look at the view (obviously) but I suppose it's possible.
-
Hi
I am still wondering. Sorry.
Lets say If I dont have drag&drop.I simply append to my internal list used in model.
How will I tell the view(s) that?
I though dataChanged would do it but not even begin/endInsertRow
does it.Thanks
-
On a side note, your rowCount implementation should rather return the size of your TableList, that way it's one less thing to manage.