首页 > 解决方案 > QML TreeView - 更改展开图标的位置

问题描述

我目前正在尝试更改QML TreeView的布局,以便展开图标(您必须单击箭头或三角形才能显示或隐藏子元素)。

我尝试将图标锚定在 TreeView 的右侧,但由于某种原因它不起作用,尽管如此,它仍然在左侧。你有什么提示吗?

非常感谢!

TreeView {
    id: treeView
    anchors.fill: parent
    model: treeModResult

    style: TreeViewStyle {
        branchDelegate: Rectangle {
            width: 15; height: 15
            color: "#00000000"
            anchors.right: treeView.right

            Image {
                id: expandArrow
                source: styleData.isExpanded ? "qrc:/img/icn_arrow_top.svg" : "qrc:/img/icn_arrow_bottom.svg"
                sourceSize.width: parent.width
                sourceSize.height: parent.height
            }

            ColorOverlay {
                anchors.fill: expandArrow
                source: expandArrow
                color: "#293147"
            }
        }
    }
}

更新

这是我目前拥有的:

在此处输入图像描述

这就是我想要的:

在此处输入图像描述

标签: qttreeviewqml

解决方案


我有同样的问题,我想出了以下解决方案,因为锚定对我也不起作用..

style: TreeViewStyle {
        branchDelegate: Rectangle{
            id: expandIcon
            x: control.width - width

control.width 表示树视图的宽度。


推荐阅读