PDA

View Full Version : [+] does not show up immediately



yuriry
4th October 2008, 10:09
I have a slight problem displaying [+] signs in a standard tree view.

The view is connected to a custom tree model. The model is initially empty. When rowCount() for some tree node is called first time the model submits a request to a separate thread using a queued connection and returns 0. The thread in the meantime loads data from the database and sends a response back to the model, aslo using a queued connection. When the model processes the response on the GUI thread, it calls beginInsertRows(), inserts the node's children and calls endInsertRows(). But [+] sing does not show up under the corresponding node in the tree view. However, as soon as I change focus to another window the [+] sign appears at the right place.

Just wanted to ask if anyone has experienced similar problem? Is there a way make [+] sing appear right away?

PS: I verified that when endInsertRows() is called, rowCount() is also called before endInsertRows() returns, and this rowCount() call returns correct number of rows, namely, the number of records retrieved from the database.

PPS: I also move tree nodes around using drag/drop. Re-parenting also goes through the same path: tree view -> tree model -> thread -> database ->thread ->tree model -> tree view. When a response arrives to the tree model, the model removes the moved node from the old position and inserts it to a new position. And there is no problem displaying [+] signs by the tree view in this scenario, it only manifests itself when the model is initially populated.

aamer4yu
4th October 2008, 13:07
may be u can try calling model->reset() after u populate data.

yuriry
4th October 2008, 19:02
This is a very good point. I actually forgot to mention that I tried calling model->reset() and the [+] signs appear properly, but the problem with this is, in addition to performance penalty, the view looses current selection. Since the model populated incrementally, I expand some node while it is selected, and suddenly selection disappears. This becomes especially annoying when I use a keyboard instead of a mouse.

aamer4yu
4th October 2008, 19:59
How about emit dataChanged() instead of reset() ??
I dont think dataChanged() removes the selection.
I am not very good at model/view, but emit dataChanged had worked for me in one case.

yuriry
4th October 2008, 20:14
Unbelievable, thank you so much! Emitting dataChanged() on the parent after calling beginInsertRows()/endInsertRows() on the same parent did the work!

What's interesting is if I turn queued connection into direct connection, thus by-passing the thread and calling the database synchronously, everything works. It only did not work with asynchronous database requests, but dataChanged() solved this. Thank you again!