Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt 如何加载相同的QML文件并传递不同的属性_Qt_Qml - Fatal编程技术网

Qt 如何加载相同的QML文件并传递不同的属性

Qt 如何加载相同的QML文件并传递不同的属性,qt,qml,Qt,Qml,我有一个index.qml文件,它由两个矩形组成,以3:1的比例填充屏幕。在这两个文件中,我加载相同的window.qml文件。我需要的是将不同的值赋给property ORIENT,以便window.qml的行为会有所不同 看看下面的例子。这个ORINET只是为了展示我想要实现的 索引.qml Rectangle { id: rootA anchors.left: parent.left anchors.top: parent.top width: parent

我有一个index.qml文件,它由两个矩形组成,以3:1的比例填充屏幕。在这两个文件中,我加载相同的window.qml文件。我需要的是将不同的值赋给property ORIENT,以便window.qml的行为会有所不同

看看下面的例子。这个ORINET只是为了展示我想要实现的

索引.qml

Rectangle {
    id: rootA
    anchors.left: parent.left
    anchors.top: parent.top
    width: parent.width * 0.65

    // set some property or function so it will be seen in loaded window.qml
    // sth like:
    property ORINET: "horizontal"

    Loader {
        anchors.left: parent.left; anchors.top: parent.top;
        anchors.right: parent.right; anchors.bottom: parent.bottom
        source: "window.qml"
    }
}

Rectangle {
    id: rootB
    anchors.right: parent.right
    anchors.top: parent.top
    width: parent.width * 0.35

    // set some property or function so it will be seen in loaded window.qml
    // sth like:
    property ORINET: "vertical"

    Loader {
        anchors.left: parent.left; anchors.top: parent.top;
        anchors.right: parent.right; anchors.bottom: parent.bottom
        source: "window.qml"
    }
}
Rectangle {
    id: windowBox
    state [
        ...
    ]
    ...
    Component.onCompleted: {
        windowBox.state = ORINET
    }
}
window.qml

Rectangle {
    id: rootA
    anchors.left: parent.left
    anchors.top: parent.top
    width: parent.width * 0.65

    // set some property or function so it will be seen in loaded window.qml
    // sth like:
    property ORINET: "horizontal"

    Loader {
        anchors.left: parent.left; anchors.top: parent.top;
        anchors.right: parent.right; anchors.bottom: parent.bottom
        source: "window.qml"
    }
}

Rectangle {
    id: rootB
    anchors.right: parent.right
    anchors.top: parent.top
    width: parent.width * 0.35

    // set some property or function so it will be seen in loaded window.qml
    // sth like:
    property ORINET: "vertical"

    Loader {
        anchors.left: parent.left; anchors.top: parent.top;
        anchors.right: parent.right; anchors.bottom: parent.bottom
        source: "window.qml"
    }
}
Rectangle {
    id: windowBox
    state [
        ...
    ]
    ...
    Component.onCompleted: {
        windowBox.state = ORINET
    }
}
根据这一点,您可以为
加载程序
提供一个属性,该属性也可用于加载的项目:

Loader {
    anchors.fill: parent
    property int ORINET: "vertical
    source: "window.qml"
}
window.qml:

Rectangle {

    states: [
        ...
    ]
    state: ORINET
}

请注意,在QtCreator中,
状态:ORINET
可能看起来斜体和蓝色,好像它不存在,但这只是编辑器

我没有根据您的示例进行调整,因为我不完全理解您计划如何使用状态,是不是QML状态机制?@pi.314我编辑了我的答案,以反映您的情况。还请注意,您可以只做
锚定。填充:父项
,而不是全部四个边分别;-)