Qt-interest Archive, May 2007
changePersistentIndexList?
Message 1 in thread
This is my third attempt to get behind the secrets of
QPersistentModelIndex and QAbstractItemModel::changePersistentIndex. So
please, if any of you knows how to use this class and method, help me!
My question is simple: am I supposed to use #changePersistentIndex in
custom subclasses of QAbstractItemModel at all? And if so, should I use
it before the internal structure is changed, or afterwards? Is there any
doc on that topic?
Many thanks.
M
--
[ signature omitted ]
Message 2 in thread
QMartin wrote:
> This is my third attempt to get behind the secrets of
> QPersistentModelIndex and QAbstractItemModel::changePersistentIndex. So
> please, if any of you knows how to use this class and method, help me!
>
> My question is simple: am I supposed to use #changePersistentIndex in
> custom subclasses of QAbstractItemModel at all? And if so, should I use
> it before the internal structure is changed, or afterwards? Is there any
> doc on that topic?
>
Hi QMartin,
Like Benjamin said, if you call
{begin,end}{Insert,Remove}{Rows,Columns}(), there's logic in
QAbstractItemModel that will take care of updating the persistent
indexes for you. Likewise, QItemSelectionModel hooks into signals of
your model to make sure that selections associated with the model are
kept consistent. If, however, you are changing the layout of your model
in a custom way, you are responsible for updating the persistent
indexes. Typically, this goes something like the following: You emit
layoutAboutToBeChanged(); you remember the "old" persistent indexes; you
alter your model, determining the "new" persistent indexes in the
process; you change the persistent indexes from old to new; finally, you
emit layoutChanged().
Example: You are implementing sorting in your model. Say there's a
persistent index with row=0 before the sorting is performed, and the
item in row 0 is moved to row 10 due to the sorting; you then want to
change the persistent index so that its row=10. If you failed to do
this, and the persistent index represented, say, a selected row in a
view, then the selection would remain at row 0 after sorting, even
though that row now contained a different item -- probably not what you
wanted. If you have the Qt sources, you can take a look at
QListModel::sort() in src/gui/itemviews/qlistwidget.cpp to see how
updating of persistent indexes can be done using
changePersistentIndexList().
Regards,
Kent
> Many thanks.
>
> M
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>
--
[ signature omitted ]
Message 3 in thread
Many thanks, Kent, I'll try that :)
Kent Hansen wrote:
> QMartin wrote:
>> This is my third attempt to get behind the secrets of
>> QPersistentModelIndex and QAbstractItemModel::changePersistentIndex. So
>> please, if any of you knows how to use this class and method, help me!
>>
>> My question is simple: am I supposed to use #changePersistentIndex in
>> custom subclasses of QAbstractItemModel at all? And if so, should I use
>> it before the internal structure is changed, or afterwards? Is there any
>> doc on that topic?
>>
> Hi QMartin,
> Like Benjamin said, if you call
> {begin,end}{Insert,Remove}{Rows,Columns}(), there's logic in
> QAbstractItemModel that will take care of updating the persistent
> indexes for you. Likewise, QItemSelectionModel hooks into signals of
> your model to make sure that selections associated with the model are
> kept consistent. If, however, you are changing the layout of your model
> in a custom way, you are responsible for updating the persistent
> indexes. Typically, this goes something like the following: You emit
> layoutAboutToBeChanged(); you remember the "old" persistent indexes; you
> alter your model, determining the "new" persistent indexes in the
> process; you change the persistent indexes from old to new; finally, you
> emit layoutChanged().
>
> Example: You are implementing sorting in your model. Say there's a
> persistent index with row=0 before the sorting is performed, and the
> item in row 0 is moved to row 10 due to the sorting; you then want to
> change the persistent index so that its row=10. If you failed to do
> this, and the persistent index represented, say, a selected row in a
> view, then the selection would remain at row 0 after sorting, even
> though that row now contained a different item -- probably not what you
> wanted. If you have the Qt sources, you can take a look at
> QListModel::sort() in src/gui/itemviews/qlistwidget.cpp to see how
> updating of persistent indexes can be done using
> changePersistentIndexList().
>
> Regards,
> Kent
>
>> Many thanks.
>>
>> M
>>
>> --
>> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
>> List archive and information: http://lists.trolltech.com/qt-interest/
>>
>>
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
--
[ signature omitted ]
Message 4 in thread
Hi again. Unfortunately, I didn't manage to update my persistent model indices using your instructions. I think this only works for sorting, i.e. when changing the position inside the same parent, but not when 'moving' an index to a different parent. In the sorting case, all data required for updating is the old and the new row and column, and that is covered by QModelIndex. But when moving stuff, you also would need to know the old and new parent index, and that info is not stored in QModelIndex.
So to give a concrete example...
1. I have a tree model with two top-level items:
-Item1
+-a
+-b
-Item2
+-c
+-d
2. I store a persistent model index of Item1 and use it as root index of a list view.
3. I move Item2 inside Item1:
-Item1
+-a
+-b
+-Item2
+-c
+-d
While moving the item, I call changePersistentIndex with the old and new index of Item1. How should the model update the persistent model index? It doesn't know the parent of the old index... It seems that the old persistent index of Item2 gets invalid, so the list view has an invalid item as root and thus shows the model's root level (in the example, only Item1).
I really have no idea how to solve this. Is this possibl at all?
Best,
M
-----Ursprüngliche Nachricht-----
Von: Kent Hansen [mailto:khansen@xxxxxxxxxxxxx]
Gesendet: Mittwoch, 09. Mai 2007 12:32
An: qt-interest@xxxxxxxxxxxxx
Betreff: Re: changePersistentIndexList?
QMartin wrote:
> This is my third attempt to get behind the secrets of
> QPersistentModelIndex and QAbstractItemModel::changePersistentIndex.
> So please, if any of you knows how to use this class and method, help me!
>
> My question is simple: am I supposed to use #changePersistentIndex in
> custom subclasses of QAbstractItemModel at all? And if so, should I
> use it before the internal structure is changed, or afterwards? Is
> there any doc on that topic?
>
Hi QMartin,
Like Benjamin said, if you call
{begin,end}{Insert,Remove}{Rows,Columns}(), there's logic in QAbstractItemModel that will take care of updating the persistent indexes for you. Likewise, QItemSelectionModel hooks into signals of your model to make sure that selections associated with the model are kept consistent. If, however, you are changing the layout of your model in a custom way, you are responsible for updating the persistent indexes. Typically, this goes something like the following: You emit layoutAboutToBeChanged(); you remember the "old" persistent indexes; you alter your model, determining the "new" persistent indexes in the process; you change the persistent indexes from old to new; finally, you emit layoutChanged().
Example: You are implementing sorting in your model. Say there's a persistent index with row=0 before the sorting is performed, and the item in row 0 is moved to row 10 due to the sorting; you then want to change the persistent index so that its row=10. If you failed to do this, and the persistent index represented, say, a selected row in a view, then the selection would remain at row 0 after sorting, even though that row now contained a different item -- probably not what you wanted. If you have the Qt sources, you can take a look at
QListModel::sort() in src/gui/itemviews/qlistwidget.cpp to see how updating of persistent indexes can be done using changePersistentIndexList().
Regards,
Kent
> Many thanks.
>
> M
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>
--
[ signature omitted ]
Message 5 in thread
> Hi again. Unfortunately, I didn't manage to update my persistent model
> indices using your instructions. I think this only works for sorting,
i.e.
> when changing the position inside the same parent, but not when
'moving'
> an index to a different parent. In the sorting case, all data required
for
> updating is the old and the new row and column, and that is covered by
> QModelIndex. But when moving stuff, you also would need to know the
old
> and new parent index, and that info is not stored in QModelIndex.
What's wrong with QModelIndex::parent() ?
Cheers,
Peter
--
[ signature omitted ]
Message 6 in thread
Peter Prade wrote:
>> Hi again. Unfortunately, I didn't manage to update my persistent model
>> indices using your instructions. I think this only works for sorting,
> i.e.
>> when changing the position inside the same parent, but not when
> 'moving'
>> an index to a different parent. In the sorting case, all data required
> for
>> updating is the old and the new row and column, and that is covered by
>> QModelIndex. But when moving stuff, you also would need to know the
> old
>> and new parent index, and that info is not stored in QModelIndex.
>
> What's wrong with QModelIndex::parent() ?
Either the old or the new index is invalid when passing them to
#changePersistentIndex, so #parent won't work for one of them. I have no
clue how #changePersistentIndex is implemented (actually I don't care
too much), I just think that with the info it is supplied (old and new
index), it cannot update a persistent model index that changed its parent.
--
[ signature omitted ]
Message 7 in thread
QMartin wrote:
> Peter Prade wrote:
>>> Hi again. Unfortunately, I didn't manage to update my persistent model
>>> indices using your instructions. I think this only works for sorting,
>> i.e.
>>> when changing the position inside the same parent, but not when
>> 'moving'
>>> an index to a different parent. In the sorting case, all data required
>> for
>>> updating is the old and the new row and column, and that is covered by
>>> QModelIndex. But when moving stuff, you also would need to know the
>> old
>>> and new parent index, and that info is not stored in QModelIndex.
>> What's wrong with QModelIndex::parent() ?
>
> Either the old or the new index is invalid when passing them to
> #changePersistentIndex, so #parent won't work for one of them. I have no
> clue how #changePersistentIndex is implemented (actually I don't care
> too much), I just think that with the info it is supplied (old and new
> index), it cannot update a persistent model index that changed its parent.
Ok... is it really that I cannot make myself clear? If my question
doesn't make sense, please tell me so and I'll try to rephrase. If
really no one has an idea how to use a persistent index with drag &
drop, could someone tell me so either? It really, really is frustrating
to seemingly be the first and only one who works with this stuff...
M
--
[ signature omitted ]
Message 8 in thread
I'm sorry, i don't see the problem you have there.
QMartin wrote:
> Either the old or the new index is invalid when passing them to
> #changePersistentIndex, so #parent won't work for one of them. I have
no
> clue how #changePersistentIndex is implemented (actually I don't care
> too much),
maybe you should! it's just 20 lines of code.
> I just think that with the info it is supplied (old and new index),
> it cannot update a persistent model index that changed its parent.
> Ok... is it really that I cannot make myself clear? If my question
> doesn't make sense, please tell me so and I'll try to rephrase. If
> really no one has an idea how to use a persistent index with drag &
> drop, could someone tell me so either? It really, really is
frustrating
> to seemingly be the first and only one who works with this stuff...
what exactly is the problem you're having?
Kent Hansen wrote:
> wanted. If you have the Qt sources, you can take a look at
> QListModel::sort() in src/gui/itemviews/qlistwidget.cpp to see how
did you check this out?
Cheers,
Peter
--
[ signature omitted ]