PDA

View Full Version : [qt4,win,g++] qpersistentmodelindex - qmodelindex



jh
3rd May 2006, 21:14
hi,

i have a model which is displayed by a treeview. the model subclasses
QAbstractItemModel.
using a selection model i can delegate the selection from the view to the
model. but how do i delegate the selection from the model to the view?
to call select() (QItemSelection) i need a modelindex. to get a modelindex
i need a row, a column and a parent which i do not have,
traversing the model to get these is to time consuming. i thought about
storing a persistent model index for each item but this sounds memory
consuming to me a i suppose there are better ways.
any hints?

best regards,
jh

wysota
4th May 2006, 20:50
Could you say what do you need it for? In general, the selection is an attribute of a view. The model just holds the data. You shouldn't keep the selection in the data model.

jh
7th May 2006, 16:00
I have a model which contains geometric elements such as solids,
lines, text, triangles and more. These elements are in containers so i get a
hierarchy. The elements can be selected by clicking
in a graphical representation (using OpenGL) but also clicking on an item in a tree view.
If the user selects an element in the treeview the OpenGL view should show the
element selected and vice versa. I have model items which represents the
model (each item got a pointer to an graphical element). These model items
are stored as internal pointers in a QModelIndex. If any item is selected in the
tree i get some QModelIndex, access the items by calling internalPointer() and
can delegate the selection to the OpenGL view. If the user selects in the OpenGL
view it's more diffucult. I get the model items but have to indices (row,column,parent)
to create a QModelIndex which i can pass to the selection model. My solution would be
to store for each model item a persistent model index (QPersistentModelIndex)
but this it not very efficient since i have to store the core item, the model item
plus the persistent model index.

In qt3 i created a subclass of treeviewitems which stored a pointer to the
graphics element. If the selection changed or the user clicked on an item this item
was passed as an argument to the accordant slots. So it was very easy to change the
selection in any other view.

Any ideas to solve this problem?

best regards,
jh

wysota
7th May 2006, 17:07
Use a persistant model index. It's the simplest solution. Otherwise you'd have to map view coordinates to model item indices.