【问题标题】:How to show all the children items in QTreeView?如何在 QTreeView 中显示所有子项?
【发布时间】:2019-02-14 02:11:57
【问题描述】:

我尝试从 Qt 创建一个 QTreeView 作为示例

http://doc.qt.io/qt-5/qtwidgets-itemviews-editabletreemodel-example.html

当我们单击每个“父”项之前的三角形时,将显示“子”项。

在我的例子中,我将此行添加到树视图中

 myTreeView->setRootIsDecorated(false);

因此,每个“父”项之前不再有三角形。

但“子”项也不再显示。

我的要求是:

  • 禁用每个“父”项之前的三角形
  • 显示树中的所有项目,包括“父”和“子”

我该怎么做?

【问题讨论】:

  • QTreeView::expandAll 是您要找的吗?
  • @G.M.:非常感谢! :)
  • 确保每次更改模型数据时都调用该方法
  • @G.M.我认为将您的解决方案作为答案发布会很有用,因此可以关闭问题并为未来的访问者提供详细记录

标签: qt qtreeview


【解决方案1】:

根据评论,您可以通过调用QTreeView::expandAll...以编程方式确保树中的所有项目都可见...

myTreeView->expandAll();

请注意,在将子项添加到模型时可能需要再次调用它,这取决于模型的大小,可能会成为性能瓶颈。

作为替代方案,最好从QTreeView 继承并覆盖QTreeView::rowsInserted 成员。

virtual void MyTreeView::rowsInserted (const QModelIndex &parent, int start, int end) override
  {

    /*
     * Let the base class do what it has to.
     */
    QTreeView::rowsInserted(parent, start, end);

    /*
     * Make sure the parent model index is expanded.  If it already
     * is expanded then the following should just be a noop.
     */
    expand(parent);
  }

这应该为大型模型提供更好的性能。

【讨论】:

    猜你喜欢
    • 2015-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    相关资源
    最近更新 更多