我是新手,我正试着让孩子的窗户上班。我跟着控件库示例后,在我看来它不能正常工作。当您单击按钮以显示子窗口时,它工作正常,如果您单击关闭它的按钮,它会执行window1.visible = false,这可以正常工作。
但是,如果不是单击按钮而是使用窗口上的x框关闭窗口,它会破坏窗口,如果您尝试再次打开它,它会显示一个窗口,但缺少控件。
该程序在main.qml开始之前初始化子窗口。在我看来,如果我在实际点击按钮时初始化窗口,那就行了,但我无法弄清楚如何做到这一点。
有什么想法吗?
以下是代码示例:
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Window 2.0
ApplicationWindow {
title: qsTr("Child Window")
width: 640
height: 480
menuBar: MenuBar {
Menu {
title: qsTr("File")
MenuItem {
text: qsTr("Exit")
onTriggered: Qt.quit();
}
}
}
Window {
id: window1
width: 200
height: 200
title: "child window"
flags: Qt.Dialog
Rectangle {
color: syspal.window
anchors.fill: parent
Text {
id: dimensionsText
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: parent.top
width: parent.width
horizontalAlignment: Text.AlignHCenter
}
Text {
id: availableDimensionsText
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: dimensionsText.bottom
width: parent.width
horizontalAlignment: Text.AlignHCenter
}
Text {
id: closeText
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: availableDimensionsText.bottom
text: "Child Window Test"
}
Button {
anchors.horizontalCenter: closeText.horizontalCenter
anchors.top: closeText.bottom
id: closeWindowButton
text:"Close"
width: 98
tooltip:"Press me, to close this window again"
onClicked: window1.visible = false
}
}
}
Button {
text: qsTr("Open Child Window")
anchors.centerIn: parent
onClicked: {
window1.visible = !window1.visible
}
}
}
如果单击子窗口中的按钮将其关闭,则可以正常工作。如果单击角落中的x关闭窗口,它会关闭它,但下次打开它时,控件(关闭窗口按钮和文本)将不可见。