- All (478)
- jom (0)
- Qt Linguist (7)
- Qt Eclipse Integration (9)
- Qt Designer (7)
- Qt Creator (4)
- Qt build system: qmake (31)
- Qt build system: configure (3)
- Qt Assistant (5)
- Printing (4)
- Porting from Qt 3 to Qt 4 (1)
- Plugins (7)
- Qt Visual Studio AddIn (2)
- Qt/MFC Migration (2)
- QtScript (3)
- MDI (2)
- XML (1)
- Widgets (22)
- WebKit (5)
- Tools and Containers (2)
- Threads (2)
- Text Handling (10)
- SQL (6)
- QtTest (1)
- QtService (1)
- Platform: Windows (49)
- Platform: Unix (16)
- Platform: Mac OS X (18)
- Image Formats (2)
- I/O (2)
- Graphicsview (8)
- Font handling (9)
- Event System (18)
- Drag and Drop (4)
- Dialogs (6)
- Desktop integration (3)
- ActiveQt (3)
- Itemviews (60)
- Layout (4)
- Qt Quick (10)
- Qt SDK (1)
- Licensing (2)
- Platform: Embedded Linux (38)
- Painting (32)
- OpenGL (4)
- Object Model (6)
- Network (5)
- Multimedia (3)
- Miscellanous (23)
- Main Window (19)
- Look and Feel (23)
- Development (0)
- Getting Involved (0)
- Routines (0)
How can a QModelIndex be retrived from the model for an internal data item?
In order to get a QModelIndex from the model for an internal data item there
are 3 options to consider:
Option 1: Use unique identifiers
Add a member to each item in the data structure that is used to store a unique
identifier for the item. Then reimplement QAbstractItemModel::data() to return
that value for a custom item data role that you can specify for your model.
Now you can use QAbstractItemModel::match() to retrieve the QModelIndex for
your item, using the unique identifier as a search parameter. If you have
editable items, you should reimplement setData() as well.
Please see the following web pages for more information:
http://qt.nokia.com/doc/qabstractitemmodel.html#data
http://qt.nokia.com/doc/qabstractitemmodel.html#match
http://qt.nokia.com/doc/qt.html#ItemDataRole-enum
http://qt.nokia.com/doc/qabstractitemmodel.html#setData
Option 2: Use QPersistentModelIndex
Create a QPersistentModelIndex for each item you wish to keep track of when
you add the item to the model. QPersistentModelIndex can be stored, and it is
used to keep a reference to an item that is valid for as long as the item can
be accessed by the model. The QPersistentModelIndex can be used as a
QModelIndex.
Please see the following web pages for more information:
http://qt.nokia.com/doc/qpersistentmodelindex.html
Option 3: Traverse the model from the root item
Store a pointer to your item’s parent, if it has any. Then store pointers to
the parent’s parent all the way up to the root item. Once you have found the
root item, you can start traversing the model, using
QAbstractItemModel::index(). For each returned index, compare its internal
pointer with the pointer of the ancestor of your item, and if they match, you
can repeat the process, calling index() with the newly found model index, in
effect dropping down one level in you data structure and getting closer to
your item. When the internal pointer of a model index matches that of your
item’s parent, you call index() one last time with that model index as the
argument, and you should get the QModelIndex for your item. Please consider
that in a worst case scenario you will traverse the whole data model.

No comments