在布局QML中使用Loader对象

时间:2014-01-24 04:03:05

标签: qt qml

我希望以这种形式在ColumnLayout对象中使用loader对象:

Item{
width: 500
height: 300
focus: true
Keys.onLeftPressed: state = "matrix"
ColumnLayout{
    id: panel
    anchors.fill: parent

    RowLayout{
        Layout.minimumHeight: 30

        Text {
            Layout.fillWidth: true
            text: "some"
        }
        Slider{
            Layout.fillWidth: true
        }
        Button {
            text: "otro"
        }
    }
    Loader {
        id: load
        Layout.fillWidth: true
        height: 30
        width: 30
    }
}
states: [
    State {
        name: "matrix"
        PropertyChanges {
            target: load
            sourceComponent: tab

        }
    }
]
Component {
    id:tab
    Rectangle {
        color: "red"
        height: 30
        width: 30
    }
}
}

我使用键事件来更改state属性,但是“tab”组件不会加载到根项目中。 任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

Loader不能有layout.fillWidth。它必须在组件加载时设置:

Item{
    width: 500
    height: 300
    focus: true
    Keys.onLeftPressed: state = "matrix"
    ColumnLayout{
        id: panel
        anchors.fill: parent

        RowLayout{
            Layout.minimumHeight: 30

            Text {
                Layout.fillWidth: true
                text: "some"
            }
            Slider{
                Layout.fillWidth: true
            }
            Button {
                text: "otro"
            }
        }
        Loader { id: load } // no property here
    }
    states: [
        State {
            name: "matrix"
            PropertyChanges {
                target: load
                sourceComponent: tab;
                Layout.fillWidth: true;  // the layout is set here
            }
        }
    ]
    Component {
        id:tab
        Rectangle {
            color: "red"
            width:30
            height:30;
        }
    }
}