QTreeView多列,可能吗?
我将 QStandardItemModel 与 QTreeView 一起使用,我希望左窗格显示节点,右窗格显示节点的值,即第 0 列和第 1 列在本例中为 1。
节点的构建非常成功,但是当我尝试使用 QStandardItem::insertRow(1, XX) 将值放入该模型时,该项目根本没有显示,是否有什么问题我错过了?
@update:
由于我递归地创建节点,所以我使用:
void Widget::addNode(QStandardItem *parent, const QVariant & data)
{
QStandardItem *childKey = ...; // left pane
QStandardItem *childValue = ...; // right pane
parent->appendRow (childKey);
}
我不能只使用 model.setItem()
来附加 childValue
,因为它转到了错误的行,并且当添加新节点时,QTreeView 默认不展开。
I'm using QStandardItemModel
with a QTreeView
, I wanted the left pane to show the nodes, and the right pane to show value of the node, which is column 0 and column 1 in this case.
Construction of nodes was pretty successful, but when I'm trying to place values into that model with QStandardItem::insertRow(1, XX)
, the item wasn't showing up at all, is there something I missed?
@update:
Since i recursively create nodes , i use:
void Widget::addNode(QStandardItem *parent, const QVariant & data)
{
QStandardItem *childKey = ...; // left pane
QStandardItem *childValue = ...; // right pane
parent->appendRow (childKey);
}
I can't just use model.setItem()
to append childValue
, since it went to the wrong row , and QTreeView is not expanded by default, when new node is appended.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查您的代码,确保您已告诉模型您想要多少列,即您已调用 QStandardItemModel::setColumnCount(),告诉模型有关额外列的信息。
编辑
然后您需要设置每列中每个项目的值。一种方法是使用
QStandardItemModel::setItem (int row, int columns, QStandardItem * item)
Check your code to make sure that you have told the model how many columns that you want, i.e. that you have called QStandardItemModel::setColumnCount(), to tell the model about the extra columns.
Edit
Then you need to set the value of each item in each column. One way to do this is to use
QStandardItemModel::setItem ( int row, int column, QStandardItem * item
)您可以一次向同一子节点的父节点添加多列,如下所示:
You can add multiple columns at once to a parent for the same child node like this: