问题是包含Button的Item没有设置高度 . 在调试布局问题时,首先要检查这类问题 . 您可以通过打印项目的几何图形来完成此操作:
Item {
Layout.columnSpan: 2
Layout.fillWidth: true
Component.onCompleted: print(x, y, width, height)
Button {
anchors.centerIn: parent
text: "Enter"
onClicked: {
loginWindow.close();
}
}
}
这输出:
qml:0 87 118 0
修复:
Item {
Layout.columnSpan: 2
Layout.fillWidth: true
implicitHeight: button.height
Button {
id: button
anchors.centerIn: parent
text: "Enter"
onClicked: {
loginWindow.close();
}
}
}
完整的代码:
import QtQuick 2.2
import QtQuick.Window 2.0
import QtQuick.Controls 1.1
import QtQuick.Layouts 1.1
Window {
id: loginWindow
property string username: login.text;
property string password: password.text;
property bool issave: savePassword.checked;
flags: Qt.Dialog
modality: Qt.WindowModal
width: 400
height: 160
minimumHeight: 160
minimumWidth: 400
title: "Login to program"
GridLayout {
columns: 2
anchors.fill: parent
anchors.margins: 10
rowSpacing: 10
columnSpacing: 10
Label {
text: "Login"
}
TextField {
id: login
text: "blah"
Layout.fillWidth: true
}
Label {
text: "Password"
}
TextField {
id: password
text: "blah"
echoMode: TextInput.Password
Layout.fillWidth: true
}
Label {
text: "Save password?"
}
CheckBox {
id: savePassword
}
Item {
Layout.columnSpan: 2
Layout.fillWidth: true
implicitHeight: button.height
Button {
id: button
anchors.centerIn: parent
text: "Enter"
onClicked: {
loginWindow.close();
}
}
}
}
}