QTreeView多列,可能吗?

发布于 2024-12-18 17:40:08 字数 575 浏览 7 评论 0原文

我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

朕就是辣么酷 2024-12-25 17:40:08

检查您的代码,确保您已告诉模型您想要多少列,即您已调用 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)

Sets the item for the given row and column to item. The model takes ownership of the item. If necessary, the row count and column count are increased to fit the item. The previous item at the given location (if there was one) is deleted.

空气里的味道 2024-12-25 17:40:08

您可以一次向同一子节点的父节点添加多列,如下所示:

void Widget::addNode(QStandardItem *parent, const QVariant & data)
{
     QStandardItem *childKey = ...; // left pane
     QStandardItem *childValue = ...; // right pane

     QList<QStandardItem*> childColumns;
     childColumns<< childKey << childValue;

     parent->appendRow(childColumns);

}

You can add multiple columns at once to a parent for the same child node like this:

void Widget::addNode(QStandardItem *parent, const QVariant & data)
{
     QStandardItem *childKey = ...; // left pane
     QStandardItem *childValue = ...; // right pane

     QList<QStandardItem*> childColumns;
     childColumns<< childKey << childValue;

     parent->appendRow(childColumns);

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文