我遇到了一个问题,我希望是因为我不善于编写QML而不是因为Qt中的一些基本错误。
每当我在水平方向上调整应用程序窗口的大小(宽度变化)时,窗口都不会调整大小到我释放鼠标的位置,但是"快照"回到最小宽度。我已设法将其删除以重现错误的最基本要求。
main.qml
import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
ApplicationWindow {
id: root
visible: true
minimumHeight: 768
minimumWidth: 1024
title: qsTr("Test")
color: "#292525"
Item {
width: 0.9*parent.width
height: 0.1*parent.height
}
}
知道为什么会这样吗?
答案 0 :(得分:6)
你有一种微妙的绑定循环形式。 QtQuickControls' ApplicationWindow
尝试保持窗口内容的大小与其中内容的大小相匹配,称为contentItem
,ApplicationWindow
的所有子项都被(默默地)重新分配到,但是你的内容大小取决于它所在的窗口。
因此,您调整窗口大小会改变Item
的大小,从而更改contentItem
的大小,这会使ApplicationWindow
调整窗口大小(与您抗争)。
这样的事可能有用:
import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
ApplicationWindow {
id: root
visible: true
minimumHeight: 768
minimumWidth: 1024
title: qsTr("Test")
color: "#292525"
// This item will just match the Window's size...
Item {
anchors.fill: parent
// ... and here, we'll fill a part of it with a rectangle.
Rectangle {
color: "red"
width: 0.9*parent.width
height: 0.1*parent.height
}
}
}