【问题标题】:Change height of rows based on item depth in QML Treeview根据 QML Treeview 中的项目深度更改行高
【发布时间】:2021-04-16 23:46:53
【问题描述】:

我正在尝试根据树中的项目深度更改 TreeView 中的行高。 itemDelegate 可以访问 depth 属性(因此我可以更改 item 的高度),但是行高只能从 rowDelegate 设置,无法看到 item 深度。

从示例中可以看出,我需要更改行高以匹配深度。 rowdelegate 如何从模型中确定项目深度?

Component {
    id: delegateMenuItem
    Column {
        height: 100  // No effect
        spacing: 20  // No effect
        bottomPadding: 20 // No effect
        Rectangle {
            border {
                color: "blue"
                width: 2
            }
            color: "red"
            height: itemText.height * 2
            width: 300
            Text {
                id: itemText
                text: qsTr(styleData.value)+"-"+height
                color: "white"
                font.pointSize: fontSize(styleData.depth)
            }
        }
    }
}

【问题讨论】:

  • 您是否尝试过改用itemDelegate
  • 我上面的代码是 itemDelegate。我还有一个 rowDelegate,它只设置行的高度。我不知道如何允许 itemDelegate 调整它显示的行的大小。
  • 所以要确认一下,使用styleData.depth 设置itemDelegateimplicitHeight 不会给你正确的大小?
  • @Pooya - 设置 ITEM 的高度,而不是 ROW。因此,如果我降低项目高度但不降低行高,则项目两侧都有很多空间。
  • 我假设如果你没有设置 rowDelegateheight 它的 implicitHeight 将被设置为最大的 implicitHeight 行中的项目。但我没试过。仅供参考,我已经使用来自市场的新 TreeView 使用 rowHeightProvider 完成了此操作。

标签: delegates qml treeview spacing


【解决方案1】:

使用QQC1中的TreeView,我建议您尝试为itemDelegate设置implicitHeight,而不是为rowDelegate设置任何高度。因此,尝试将您的 sn-p 更改为:

itemDelegate: Rectangle {
    id: delegateMenuItem

    border { color: "blue"; width: 2 }
    color: "red"
    width: ...
    implicitHeight: styleData.depth * ...
    
    Text {
        id: itemText
        
        anchors.fill: parent
        ...
    }
}

(在您当前的实现中delegateMenuItem 没有任何implicitHeight。这意味着它的height 可能是0。而且您使用的ColumnLayout 没有用,所以我已经删除了它。)

我没有尝试过上述方法。但是我已经尝试过使用TreeView from Qt marketplace 做你想做的事情。它旨在与 QQC2 一起使用,并且基于来自 QQC2 的 TableView,因此您可以使用 rowHeightProvider

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    相关资源
    最近更新 更多