[Qt-interest] Creating a QModelIndex outside of a QAbstractItemModel subclass

Dario Ahdoot dario.ahdoot at image-metrics.com
Thu Mar 12 16:36:05 CET 2009


Thanks. I somehow missed that method on QAbstractItemModel.

On a related note, based on the code snippet below, would you consider it a bug that if you have a tree with a SelectionBehavior of QAbstractItemView::SelectRows, then when you call select on it, then it should select the whole row of the model index you pass in?

-----Original Message-----
From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Andreas Pakulat
Sent: Thursday, March 12, 2009 1:02 AM
To: qt-interest at trolltech.com
Subject: Re: [Qt-interest] Creating a QModelIndex outside of a QAbstractItemModel subclass

On 11.03.09 17:11:06, Dario Ahdoot wrote:
> Is it really not possible to create a QModelIndex outside of a QAbstractItemModel subclass?
> 
> * The QModelIndex c'tor which takes any args is private
> * There are no setRow/setColumn methods
> * To create a QModelIndex, you must call QAbstractItemModel::createIndex( row, column )
> * However, createIndex is protected, so it is only callable from a subclass of QAbstractItemModel
> 
> This is rather inconvenient since it means that any time you have any piece of code that needs to create a QModelIndex you must either be in some subclass of QAbstractItemModel, or you must in your sub-class of QAbstractItemModel write a createIndex wrapper. Take for instance the following piece of code:

No, its quite easy to construct a QModelIndex with just your model
instance:

model->index(row,col,parent);

Of course this means you'll have to ask it for every index along your
parent-axis, i.e. you might end up having something like this:

model->index(0,1,model->index(1,0, model->index(2,0, model->index(0,0) ) ) )

The indexes will be created behind the scenes and thats also why
createIndex is protected. Creating a model index should be done privately
inside the model as it'll need knowledge about how the model is built
internally. Thats something you don't want to be known outside the model.

If using model->index(row,col) doesn't work for you, then you should check
wether your model works correctly. See for example the ModelTest on Qt
labs.

Andreas

-- 
You will hear good news from one you thought unfriendly to you.
_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest




More information about the Qt-interest-old mailing list