我从 QAbstractItemModel 派生来编码我自己的数据树,但 QTreeView 没有显示。
由于错误的变量寿命,我看到的大多数类似问题的答案都得到了解决,所以这是我分配模型的代码:
ui.tvHierarchy->setModel(
new MetaHierarchyModel(
cutOffExtension(
fileName.toStdString()
)
)
);
在构建模型时,它的根节点会填充数据,稍后将根据需要加载更多数据(通过fetchMore
)。
我开始输出每个被调用的函数。这是调用序列的日志:
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"
输出roleNames()
:
QHash((0, "display")(1, "decoration")(2, "edit")(3, "toolTip")(4, "statusTip")(5, "whatsThis"))
最后四行然后永远重复(或至少直到我失去耐心)。对我来说,看起来根数据已被获取,但它们从未显示出来。奇怪的是,data
-的最后一个参数是int role
13,它没有被定义为任何角色(我仍然无条件地返回有效字符串)。
实施此操作时我错过了什么吗?