4

I have derived from QAbstractItemModel to encode my own tree of data, but the QTreeView is not displaying.

Most of the answers I saw to similar questions were solved because of wrong variable life time, so here is my code for allocation of the model:

ui.tvHierarchy->setModel(
    new MetaHierarchyModel(
        cutOffExtension(
            fileName.toStdString()
        )
    )
);

On construction the model gets its root node filled with data and later shall load more data as needed (via fetchMore).

I began outputting every function that is called. This is a log of the call sequence:

columnCount( QModelIndex(-1,-1,0x0,QObject(0x0) ) ) 
    return  1 
columnCount( QModelIndex(-1,-1,0x0,QObject(0x0) ) ) 
    return  1 
columnCount( QModelIndex(-1,-1,0x0,QObject(0x0) ) ) 
    return  1 
hasChildren( QModelIndex(-1,-1,0x0,QObject(0x0) ) ) 
    return  true 
hasChildren( QModelIndex(-1,-1,0x0,QObject(0x0) ) ) 
    return  true 
canFetchMore( QModelIndex(-1,-1,0x0,QObject(0x0) ) )) 
    return  false 
rowCount( QModelIndex(-1,-1,0x0,QObject(0x0) ) ) 
    return  1 
index( 0 ,  0 ,  QModelIndex(-1,-1,0x0,QObject(0x0) ) ) 
    return  QModelIndex(0,0,0x987aa0,MetaHierarchyModel(0xa16970) )  
hasChildren( QModelIndex(0,0,0x987aa0,MetaHierarchyModel(0xa16970) )  ) 
    return  true 
columnCount( QModelIndex(-1,-1,0x0,QObject(0x0) ) ) 
    return  1 
parent( QModelIndex(0,0,0x987aa0,MetaHierarchyModel(0xa16970) )  ) 
    return  QModelIndex(-1,-1,0x0,QObject(0x0) ) 
index( 0 ,  0 ,  QModelIndex(-1,-1,0x0,QObject(0x0) ) ) 
    return  QModelIndex(0,0,0x987aa0,MetaHierarchyModel(0xa16970) )  
data( QModelIndex(0,0,0x987aa0,MetaHierarchyModel(0xa16970) )  ,  13 ) 
    return  "Metaparticle 1" 
columnCount( QModelIndex(-1,-1,0x0,QObject(0x0) ) ) 
    return  1 
parent( QModelIndex(0,0,0x987aa0,MetaHierarchyModel(0xa16970) )  ) 
    return  QModelIndex(-1,-1,0x0,QObject(0x0) ) 
index( 0 ,  0 ,  QModelIndex(-1,-1,0x0,QObject(0x0) ) ) 
    return  QModelIndex(0,0,0x987aa0,MetaHierarchyModel(0xa16970) )  
data( QModelIndex(0,0,0x987aa0,MetaHierarchyModel(0xa16970) )  ,  13 ) 
    return  "Metaparticle 1" 

Output of roleNames():

QHash((0, "display")(1, "decoration")(2, "edit")(3, "toolTip")(4, "statusTip")(5, "whatsThis")) 

The last four lines are then repeated forever (or at least until I loose my patience). To me it looks like the root data are fetched, but they never get displayed. It is strange, that the last parameter of data - which is int role, has the value 13, which is not defined as any role (still I give back valid strings unconditionally).

Is there anything I missed when implementing this?

3
  • As a rule, you want to be as stingy as possible when returning values from your data function. If you check the docs, you can see that role 13 is Qt::SizeHintRole - I'd imagine your view is getting rather confused when that returns a string! Commented Aug 31, 2012 at 18:21
  • @XavierHolt You are right. I changed this after it did not work, when I was returning only on the DisplayRole and an QVariant() else. Presumably I had another error back then and now was having this problem all the way. Would you please post it as an answer, so I can mark it correct? Commented Aug 31, 2012 at 18:26
  • Great! Glad that worked - I'll have an answer up in a minute. Commented Aug 31, 2012 at 19:48

1 Answer 1

9

The QAbstractItemModel::data function should be as stingy as possible with the data it returns. Make sure you only return data when you have an exact match on the display role and column number. In all other cases just return an invalid QVariant (just call the default constructor), and your view widget will fill in these missing values with sensible defaults.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.