Results 1 to 5 of 5

Thread: How many times is the data() function in QStandardItemModel been invoked?

  1. #1

    Default How many times is the data() function in QStandardItemModel been invoked?

    Hi all:
    i create a 2*2 table, and i create my own simple model, just re-implementing the data() function. The code is as follows:
    Qt Code:
    1. QVariant MyStandardItemModel::data(
    2. const QModelIndex & index,
    3. int role) const
    4. {
    5.  
    6. int column=index.column();
    7. qDebug()<<"row="<<index.row()<<"column="<<index.column()<<"data role="<<role;
    8. if(role==Qt::DisplayRole){
    9. //qDebug()<<"row="<<index.row()<<"column="<<index.column()<<"display="<<role;
    10. return column+3;
    11. }
    12. else return QStandardItemModel::data(index,role);
    13. }
    To copy to clipboard, switch view to plain text mode 

    when i run my program , i think the data() function should be called 4 times, but actually it is called many times,
    QQ截图20140116143644.jpg
    It is confused me.
    My questions are:
    1.why the data() function is called so many times but not 4 times?
    2.why the item has 7 roles while i don't set roles to item. For example:
    row= 0 column= 0 data role= 6
    row= 0 column= 0 data role= 7
    row= 0 column= 0 data role= 9
    row= 0 column= 0 data role= 10
    row= 0 column= 0 data role= 1
    row= 0 column= 0 data role= 0
    row= 0 column= 0 data role= 8
    Is this the default setting or i missing something?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How many times is the data() function in QStandardItemModel been invoked?

    It is called to get data for cell in each role that it requires to construct the view. There are roles for check states, icons, colours, alignment, tool tips, status tip, size hint etc. Your subclass simply passes these off to the QStandardItemModel, which returns the data it has for these roles.
    Last edited by ChrisW67; 16th January 2014 at 07:09.

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

    Default Re: How many times is the data() function in QStandardItemModel been invoked?

    Why would it be called 4 times? The model is being asked for all roles the view needs so for each item the function is called several times, as in your log. The view does not "see" your C++ code, it does not know you did not set any other roles than the display role so it asks for other roles used by the view or the delegate as well.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4

    Default Re: How many times is the data() function in QStandardItemModel been invoked?

    Thank you very much

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How many times is the data() function in QStandardItemModel been invoked?

    You can think of that as a conversation between to people, where one can do a lot of things and the other decides which of those matter.

    The view knows how to do stuff and asks the model if it wants to provide any input on those matters:

    View: Hi there, what do you want me to draw in cell 0/0?
    Model: (column+3 -> 3) 3!
    View: Right. Any specific font I should use?
    Model: No
    View: Ok, will use the standard font. Any wishes regarding the text color?
    Model: No
    View: Excellent, I hate it when models mess up the colors. Just in case, does this cell have a check state, i.e. do you want me to draw a checkbox?
    .....

    View: All right! Lets move on to cell 0/1....

    Cheers,
    _

Similar Threads

  1. Replies: 11
    Last Post: 5th September 2012, 20:47
  2. Replies: 5
    Last Post: 23rd September 2011, 09:44
  3. Replies: 1
    Last Post: 2nd June 2010, 16:14
  4. Replies: 5
    Last Post: 5th April 2009, 01:55
  5. Replies: 5
    Last Post: 6th March 2007, 05:34

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.