Qt qml -如何在其定义中使用组件

Qt qml - how to use component in its definition?

本文关键字:定义 组件 qml Qt      更新时间:2023-10-16

我必须在qml中实现TreeView。基本上,因为每个子树只是TreeView本身,我想在TreeView定义中使用TreeView组件(这是结束的Repeater)。

这是我引用我正在定义的组件的代码的一部分。

你可以看到rootDelegate实际上是Component中的id

问题是Qt给出错误无法将QQuickRow分配给QQmlComponent

 Repeater {
    model: childrens
    delegate: rootDelegate
 }

TreeView.qml

import QtQuick 2.0

Component {
    id: rootDelegate
    Column {
        Row {
            id: itemControl
            spacing: 2
            Rectangle {
                anchors.verticalCenter: parent.verticalCenter
                gradient: Gradient {
                    GradientStop { position: 0.0; color: "#EEEEEE" }
                    GradientStop { position: 1.0; color: "#404040" }
                }
                width: openChar.implicitWidth
                height: openChar.implicitHeight - 6
                radius: 3
                MouseArea {
                    anchors.fill: parent
                    cursorShape: Qt.PointingHandCursor
                }
                Text {
                    id: openChar
                    text: "+"
                    color: "black"
                    anchors.centerIn: parent
                }
            }
            Rectangle {
                height: 1
                color: "#A0A0A0"
                width: 10
                anchors.verticalCenter:  parent.verticalCenter
            }
            Text {
                text: model.text
                color: "white"
            }
        }
        Repeater {
            model: childrens
            delegate: rootDelegate
        }
    }
}

您试图在自身中递归地使用Сomponent,这在Qml

中是不允许的