PDA

View Full Version : QTreeView expansion after model reset



d_stranz
3rd December 2014, 03:53
I have a QTreeView that displays the contents of a custom QAbstractItemModel-based class. The custom model wraps a data structure that can be updated when the app recomputes results. When this happens, the model calls beginResetModel() / endResetModel(), which results in a modelReset() signal. All is fine with this part.

When the tree view receives notification of an updated model, I want to expand the root item to display all of its top-level children. So, I have made a connection between the model's modelReset() signal and a slot in the widget that contains the QTreeView. In this slot, I call QTreeView::expandToDepth( 1 ). Unfortunately, it appears that this slot is being invoked before the tree view updates itself, because I can see the view's contents disappear and then reappear (unexpanded) after leaving the slot. (I have replaced the expandToDepth() call with expandAll() with the same result).

Is there a way I can determine that the tree view has finished its internal update after a model reset so I can expand the root node?

wysota
3rd December 2014, 07:59
I would start a timer with a tiny interval upon receiving modelReset() and then expand the tree when it fires.

anda_skoa
3rd December 2014, 10:52
Are you connecting after setting the model on the view?

Otherwise you will get the reset signal before the view does.

Cheers,
_

d_stranz
3rd December 2014, 23:42
Are you connecting after setting the model on the view?

Otherwise you will get the reset signal before the view does.

You nailed it. I had thought about the connection order, but I guess I assumed I was doing it in the right order. Reversing the order fixed it.