Results 1 to 6 of 6

Thread: questions about "Custom item roles"

  1. #1
    Join Date
    Aug 2008
    Location
    Nanjing, China
    Posts
    66
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default questions about "Custom item roles"

    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.
    Jerry

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: questions about "Custom item roles"

    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.

  3. #3
    Join Date
    Aug 2008
    Location
    Nanjing, China
    Posts
    66
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: questions about "Custom item roles"

    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.
    Qt Code:
    1. QMap<int, QVariant> itemData ( const QModelIndex & index ) const
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. QMap<int, QVariant> MyModel::itemData ( const QModelIndex & index ) const{
    2. QMap<int, QVariant> m = QAbstractItemModel::itemData(index);
    3. m[MyCustomItemRole] = data(MyCustomItemRole);
    4. return m;
    5. }
    To copy to clipboard, switch view to plain text mode 

    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?
    Jerry

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: questions about "Custom item roles"

    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

  5. The following user says thank you to aamer4yu for this useful post:

    calmspeaker (10th September 2008)

  6. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: questions about "Custom item roles"

    • 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.
    J-P Nurmi

  7. The following user says thank you to jpn for this useful post:

    calmspeaker (10th September 2008)

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: questions about "Custom item roles"

    Quote Originally Posted by jpn View Post
    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.

  9. The following user says thank you to wysota for this useful post:

    calmspeaker (10th September 2008)

Similar Threads

  1. Item Delegate Painting
    By stevey in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2008, 07:37

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.