Qt QML将子属性绑定到父属性
我想通过子元素“mainLayout”的最小宽度和高度将ApplicationWindow设置为最小宽度和高度。在父QML应用程序窗口中使用“mainLayout”属性时遇到问题。我试图通过创建别名使属性可见。不确定这是否是正确的解决方案。它不起作用。但是当我运行时也没有错误 我的代码如下所示: main.qmlQt QML将子属性绑定到父属性,qt,qml,Qt,Qml,我想通过子元素“mainLayout”的最小宽度和高度将ApplicationWindow设置为最小宽度和高度。在父QML应用程序窗口中使用“mainLayout”属性时遇到问题。我试图通过创建别名使属性可见。不确定这是否是正确的解决方案。它不起作用。但是当我运行时也没有错误 我的代码如下所示: main.qml import QtQuick 2.2 import QtQuick.Controls 1.2 import QtQuick.Layouts 1.3 import QtQuick.Win
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个文件不起作用。无法使用id为
mainLayout
的QML元素的属性minimumWidth
的原因是它没有属性
但是,所讨论的QML元素确实有一个附加属性Layout.minimumWidth
,因为它是id为包装的ColumnLayout中的一个项。您已经发现可以通过serial.mainLayout.Layout.minimumWidth
访问它
启用mainLayout
的最小宽度并不是最容易理解的。简言之,它使对象能够使用其他属性进行注释,这些属性对对象不可用,但在某些情况下是相关的。在这种情况下,最小宽度被认为与ColumnLayout的子项相关。ColumnLayout中的项支持这些附加项。已解决它,最小宽度:serial.mainloayout.Layout.minimumWidth