设置窗口工具栏时,QML WindowApplication使用SplitView会产生奇怪的尺寸调整行为

时间:2018-09-26 23:43:24

标签: qt qml qtquick2 qt-quick

我在QML中陷入了这个基本问题。我必须说,即使我已经与Qt合作,但我还是QML新手。

我在ApplicationWindow中定义一个垂直的SplitView,然后尝试将底视图的高度设置为窗口高度的百分比。只要我不设置窗口工具栏,它就可以正常工作。当我这样做时,底部高度仅为零。

尝试了一些解决方法后,我设法通过添加一个额外的值来显示它,该值必须高于百分比乘以工具栏高度(屏幕上为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错误……)。

1 个答案:

答案 0 :(得分:0)

这似乎是一个错误,一种解决方法不是使用锚,而是一种简单的计算:

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
            }
        }
    }
}

enter image description here