设置 window 工具栏时,QML WindowApplication 与 SplitView 的奇怪大小调整行为
QML WindowApplication weird sizing behavior with SplitView when setting the window toolbar
我在 QML 中遇到了这个基本问题。我必须说,即使我已经使用过 Qt,我也是 QML 新手。
我在 ApplicationWindow 中定义了一个垂直的 SplitView,并尝试将底部视图的高度设置为 window 高度的百分比。只要我不设置 window 工具栏,它就可以正常工作。当我这样做时,底部高度仅为零。
在尝试了一些解决方法后,我设法通过添加一个额外值来显示它,该值必须高于百分比乘以工具栏高度(在我的屏幕上为 46 像素)。
这对我来说真的很奇怪,一点也不明显。看起来 QML 不会重新计算底部视图的高度,如果它要初始化为零,但如果它在初始化时高于零(它甚至可以使用 0.01 像素......)。
我的代码:
import QtQuick 2.0
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.1
ApplicationWindow {
id: mainWindow
width: 640; height:480
toolBar: ToolBar {}
SplitView {
orientation: Qt.Vertical
anchors.fill: parent
Component.onCompleted: { print("Split view: " + width + "x" + height) }
onHeightChanged: print("Split view: " + width + "x" + height)
Rectangle {
Layout.fillHeight: true
Text {
text: "Top"
anchors.centerIn: parent
}
}
Rectangle {
height: 0.1 * parent.height
Text {
text: "Bottom"
anchors.centerIn: parent
}
Component.onCompleted: { print("Bottom: " + width + "x" + height + " (parent " + parent.height + ")") }
onHeightChanged: print("Bottom: " + width + "x" + height + " (parent " + parent.height + ")")
}
}
}
如果有人有想法以正确的方式编写它(如果它不是 Qt 错误...),谢谢。
看来是个bug,解决方法是不使用anchor,简单计算一下:
import QtQuick 2.0
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.1
ApplicationWindow {
id: mainWindow
width: 640; height:480
visible: true
toolBar: ToolBar {
id: toolbar
}
SplitView {
height: mainWindow.height - toolBar.height // <----
width: mainWindow.width // <----
orientation: Qt.Vertical
Rectangle {
Layout.fillHeight: true
Text {
text: "Top"
anchors.centerIn: parent
}
}
Rectangle {
height: 0.1 * parent.height
Text {
text: "Bottom"
anchors.centerIn: parent
}
}
}
}
我在 QML 中遇到了这个基本问题。我必须说,即使我已经使用过 Qt,我也是 QML 新手。
我在 ApplicationWindow 中定义了一个垂直的 SplitView,并尝试将底部视图的高度设置为 window 高度的百分比。只要我不设置 window 工具栏,它就可以正常工作。当我这样做时,底部高度仅为零。
在尝试了一些解决方法后,我设法通过添加一个额外值来显示它,该值必须高于百分比乘以工具栏高度(在我的屏幕上为 46 像素)。
这对我来说真的很奇怪,一点也不明显。看起来 QML 不会重新计算底部视图的高度,如果它要初始化为零,但如果它在初始化时高于零(它甚至可以使用 0.01 像素......)。
我的代码:
import QtQuick 2.0
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.1
ApplicationWindow {
id: mainWindow
width: 640; height:480
toolBar: ToolBar {}
SplitView {
orientation: Qt.Vertical
anchors.fill: parent
Component.onCompleted: { print("Split view: " + width + "x" + height) }
onHeightChanged: print("Split view: " + width + "x" + height)
Rectangle {
Layout.fillHeight: true
Text {
text: "Top"
anchors.centerIn: parent
}
}
Rectangle {
height: 0.1 * parent.height
Text {
text: "Bottom"
anchors.centerIn: parent
}
Component.onCompleted: { print("Bottom: " + width + "x" + height + " (parent " + parent.height + ")") }
onHeightChanged: print("Bottom: " + width + "x" + height + " (parent " + parent.height + ")")
}
}
}
如果有人有想法以正确的方式编写它(如果它不是 Qt 错误...),谢谢。
看来是个bug,解决方法是不使用anchor,简单计算一下:
import QtQuick 2.0
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.1
ApplicationWindow {
id: mainWindow
width: 640; height:480
visible: true
toolBar: ToolBar {
id: toolbar
}
SplitView {
height: mainWindow.height - toolBar.height // <----
width: mainWindow.width // <----
orientation: Qt.Vertical
Rectangle {
Layout.fillHeight: true
Text {
text: "Top"
anchors.centerIn: parent
}
}
Rectangle {
height: 0.1 * parent.height
Text {
text: "Bottom"
anchors.centerIn: parent
}
}
}
}