Qt QML将子属性绑定到父属性

时间:2018-02-13 17:17:59

标签: qt qml

我想通过子元素“mainLayout”的最小宽度和高度将ApplicationWindow设置为最小宽度和高度。我无法在父QML ApplicationWindow中使用“mainLayout”的属性。我尝试通过创建别名来查看属性。不确定它是否是正确的解决方案。这是行不通的。但是当我跑步时也没有错误。

我的代码如下所示:

main.qml

import QtQuick 2.2
import QtQuick.Controls 1.2
import QtQuick.Layouts 1.3
import QtQuick.Window 2.2

ApplicationWindow {
visible: true
width: 1500
height: 1200
property int margin: 11

minimumWidth: serial.mainLayout.minimumWidth + 2 * margin //this one is not working
minimumHeight: serial.mainLayout.minimumHeight + 2 * margin //this one is not working


Serial {
        id: serial
        anchors.fill: parent
    }
}

Serial.qml

import QtQuick 2.7
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.3
import io.qt.serialComm 1.0

Item {

    anchors.fill: parent
    id: item
    property alias mainLayout: mainLayout

    ColumnLayout {
        id: wrapper
        width: parent.width/2
        height: parent.height/2

        ColumnLayout {
            id: mainLayout
            anchors.fill: parent
            anchors.margins: margin
            GroupBox {
                id: rowBox
                title: "Row layout"
                Layout.fillWidth: true

                RowLayout {
                    id: rowLayout
                    anchors.fill: parent
                    TextField {
                        placeholderText: "This wants to grow horizontally"
                        Layout.fillWidth: true
                    }
                    Button {
                        text: "Button"
                    }
                }
            }

            GroupBox {
                id: gridBox
                title: "Grid layout"
                Layout.fillWidth: true

                GridLayout {
                    id: gridLayout
                    rows: 3
                    flow: GridLayout.TopToBottom
                    anchors.fill: parent

                    Label { text: "Line 1" }
                    Label { text: "Line 2" }
                    Label { text: "Line 3" }

                    TextField { }
                    TextField { }
                    TextField { }

                    TextArea {
                        text: "This widget spans over three rows in the GridLayout.\n"
                            + "All items in the GridLayout are implicitly positioned from top to bottom."
                        Layout.rowSpan: 3
                        Layout.fillHeight: true
                        Layout.fillWidth: true
                    }
                }
            }
            TextArea {
                id: t3
                text: "This fills the whole cell"
                Layout.minimumHeight: 30
                Layout.fillHeight: true
                Layout.fillWidth: true
            }
            GroupBox {
                id: stackBox
                title: "Stack layout"
                implicitWidth: 200
                implicitHeight: 60
                Layout.fillWidth: true
                Layout.fillHeight: true
                StackLayout {
                    id: stackLayout
                    anchors.fill: parent

                    function advance() { currentIndex = (currentIndex + 1) % count }

                    Repeater {
                        id: stackRepeater
                        model: 5
                        Rectangle {
                            color: Qt.hsla((0.5 + index)/stackRepeater.count, 0.3, 0.7, 1)
                            Button { anchors.centerIn: parent; text: "Page " + (index + 1); onClicked: { stackLayout.advance() } }
                        }
                    }
                }
            }
        }
    }
}

当我将代码放在一个文件中时,它可以工作,ApplicationWindow不会小于子元素“mainLayout”的最小高度和宽度。但拆分成2个文件不起作用..

1 个答案:

答案 0 :(得分:0)

您无法使用Q minimumWidth的{​​{1}}属性mainLayout,而serial.mainLayout.minimumWidthLayout.minimumWidth,因为它没有{。}}。

但是,相关的QML元素确实具有附加属性wrapper,因为它是具有标识serial.mainLayout.Layout.minimumWidth的ColumnLayout中的项目。您已经发现可以通过mainLayout访问它。

启用var code = req.user._id; var url = 'http://localhost:4200/pauth?id='+code; res.redirect(url); 的minimumWidth的

Attached property mechanism并不是最容易理解的。简而言之,它使对象能够使用对象不可用但在某些情况下相关的额外属性进行注释。在这种情况下,minimumWidth被认为与ColumnLayout的子项相关。 ColumnLayout中的项目支持这些附加的properties