PDA

View Full Version : QAbstractItemModel reset Persistent Index



larcho
3rd April 2010, 07:14
Hi,

How do I reset a persistent Index List?

Let's say I signal "layoutAboutToBeChanged()" then I update my data and check if the current persistent index is still there. If it's not there, I would like to clear the persistent index. Is there a way to do this?

Thank you

wysota
3rd April 2010, 11:21
You can probably assign an invalid (null) model index to it. Although layoutAboutToBeChanged() shouldn't invalidate any indexes as no data gets added or removed, only its position may change.

larcho
3rd April 2010, 18:57
I'm almost sure that I'm using the layoutToBeChanged() in a wrong way. To clarify a bit what I want to do is: I have a table view that shows files on a server, which the client can assign to a certain item or folder. Once the file gets assigned, it gets removed from the table view (hence, the persistent index would be set back to a non selection).

Changing the persistent index to an empty model index list crashes the Application.

Thank you.

wysota
3rd April 2010, 22:12
Why are you using persistent indexes in the first place? In 99% of the cases you shouldn't need them at all.

larcho
4th April 2010, 04:51
But how do I keep a view from changing it's selection after updating it's data? I'm aware that the selection on a view doesn't move from it's row or column, but sometimes after updating data the order on the view gets resorted.

wysota
4th April 2010, 06:52
But how do I keep a view from changing it's selection after updating it's data?
The view will take care of that if you emit proper signals from the model (i.e. dataChanged(), rowsInserted(), rowsRemoved(), etc.). It takes a bit of effort to do it but you just have to do it.

larcho
4th April 2010, 07:07
I'll give a look into that. Thank you.

wysota
4th April 2010, 08:28
One more thing... what you might try is to connect a slot to the layoutAboutToBeChanged() signal and use QAbstractItemModel::changePersistentIndex() to update persistent indexes manually if you are able to determine new positions of each of the persistent indexes kept by the model.

larcho
5th April 2010, 06:01
I did your first approach and it worked fine. Working with dataChanged(), insertRow() and removeRow() did the trick. As you said, it´s a bit more work, but the end result is great.

Thank you.