PDA

View Full Version : questions about "Custom item roles"



calmspeaker
9th September 2008, 03:08
Exactly as "Qt Centre wiki" said:In many situations roles mentioned earlier are not sufficient to store or retrieve all the data associated with an item. In this situation model designers can create new roles and assign data to them. I need new role to expand the item data.

Before I read the wiki, I define new role like this const int DisplayTypeRole = 32; and In data() and setData(), I write code to deal with the custom role in "switch sentences", and the behavior of the program looks well.

But I really donnot know why I need to redefine this function : QMap<int, QVariant> itemData ( const QModelIndex & index ) const. What is in the structure QMap<int, QVariant>? All the Model's data? Please give me a clue.:)

aamer4yu
9th September 2008, 05:03
But I really donnot know why I need to redefine this function : QMap<int, QVariant> itemData ( const QModelIndex & index ) const.

From the docs :

Reimplemented this function if you want to extend the default behavior of this function to include custom roles in the map.

Reimplement it if u need it.

calmspeaker
9th September 2008, 09:52
er,I need make the questions more clear.
from wiki
Custom item roles
In many situations roles mentioned earlier are not sufficient to store or retrieve all the data associated with an item. In this situation model designers can create new roles and assign data to them. Custom roles have to be given numbers starting from Qt::UserRole up. Some model methods need to be redefined to teach the model to handle new content.

QMap<int, QVariant> itemData ( const QModelIndex & index ) const
This method returns a packet which contains all data associated with an item. If new roles are created their data should be added to the packet here and returned along with the rest of the roles.

The simplest reimplementation (introducing the MyModel::MyCustomItemRole role) of the itemData() method follows:

QMap<int, QVariant> MyModel::itemData ( const QModelIndex & index ) const{
QMap<int, QVariant> m = QAbstractItemModel::itemData(index);
m[MyCustomItemRole] = data(MyCustomItemRole);
return m;
}

Also other methods which take a role id as a parameter need to be altered to handle custom roles just like they handle the built in ones.

I created new role and altered all the functions which take a role id as a parameter to handle custom roles.But I didn't rewrite the itemData function. And the program seems to run well.

Is QMap another copy of model data?Because I already stored the data in the vector. When should I rewrite setItemData function?

aamer4yu
9th September 2008, 12:14
May be I need to make my answer clearer too ...
As you have experienced that ur programs well even though u didnt rewrite itemData() function.
That function is needed in cases where you want a instant map to get all the possible values of a given item at a given index (QModelIndex &index). I didnt encounter Qt using this function internally.
The most important function in implementing a model is data(). Views query data from the data() function.

Even if you look at the extended itemData() function, you will see that function first gets a map from QAbstractItemModel for the given index, and then adds it own data() for custom role. Even if u have a look at QAbstractItemModel::itemData(), you will find that it creates a map from the data() function and returns the map. And yes, data is being duplicated in case itemData function is called ( but only for a given index, not whole model).

I hope i am a bit more clear now :rolleyes:

jpn
9th September 2008, 12:25
QAbstractItemModel::itemData() is used for example for drag and drop. If you want to pass custom data roles along the data associated to a drag object, this is where to do it.
There is probably never need to reimplement QAbstractItemModel::setItemData() because the default implementation simply calls setData() for each role which should be sufficient.

wysota
9th September 2008, 20:54
There is probably never need to reimplement QAbstractItemModel::setItemData() because the default implementation simply calls setData() for each role which should be sufficient.

Unless calling setData() for each role is an overhead over a solution where you can store all data at once. Remember that setData() emits a dataChanged() signal - for 10 roles you'd get 10 such signals and 10 updates of all views.