Pergunta

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?

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top