qml - 此 QML 示例中的边距从何而来?

标签 qml qtquick2 qtquickcontrols2

我在 SO 上查看了大量关于 QML 内容边距的问题,但所有答案都指向缺少 spacing: 0 属性。我已经完成了所有这些,但仍然有一些我无法消除的奇怪空间。任何人都可以解释为什么这个 QML 代码:

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Test")

    RowLayout {
        spacing: 0
        anchors.margins: 0, 0, 0, 0
        anchors.fill: parent;

        Pane {
            anchors.margins: 0, 0, 0, 0
            id: menuPane
            anchors.top: parent.top;
            anchors.bottom: parent.bottom;
            width: 200

            ColumnLayout {
                spacing: 0
                anchors.fill: parent
                anchors.margins: 0, 0, 0, 0

                Rectangle {
                    id: testRect
                    Layout.fillWidth: true
                    anchors.top: parent.top
                    anchors.left: parent.left
                    anchors.right: parent.right
                    height: 20
                    color: "green"
                }
            }
        }

        Pane {
            anchors.margins: 0, 0, 0, 0
            anchors.left: menuPane.right
            anchors.right: parent.right
            anchors.top: parent.top
            anchors.bottom: parent.bottom

            Rectangle {
                anchors.margins: 0, 0, 0, 0
                anchors.fill: parent
                color: "black"
            }
        }
    }
}

是这样渲染的?为什么矩形之间的边距在那里? QML window screenshot

最佳答案

这个答案很简短:

将 Pane 的属性 padding 设置为 0,这样就不会没有边距了。
您还可以单独设置所有填充(leftPadding ...)这些属性继承自 Control

在你的例子中看起来像这样:

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Test")

    RowLayout {
        spacing: 0
        anchors.margins: 0
        anchors.fill: parent;

        Pane {
            anchors.margins: 0
            id: menuPane
            anchors.top: parent.top;
            anchors.bottom: parent.bottom;
            width: 200
            padding: 0


            ColumnLayout {
                spacing: 0
                anchors.fill: parent
                anchors.margins: 0

                Rectangle {
                    id: testRect
                    Layout.fillWidth: true
                    anchors.top: parent.top
                    anchors.left: parent.left
                    anchors.right: parent.right
                    height: 20
                    color: "green"
                }
            }

            Component.onCompleted: console.log(bottomPadding, leftPadding)
        }

        Pane {
            anchors.margins: 0
            anchors.left: menuPane.right
            anchors.right: parent.right
            anchors.top: parent.top
            anchors.bottom: parent.bottom
            padding: 0

            Rectangle {
                anchors.margins: 0
                anchors.fill: parent
                color: "black"
            }
        }
    }
}

关于qml - 此 QML 示例中的边距从何而来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39899090/

相关文章:

qt - ListView 中没有更新

qt - 在 QML 中截取特定项目的屏幕截图的方法是什么?

qt - QML 焦点如何传播?

javascript - QML日历: wrong number of weeks displayed in a month view

c++ - QML 对象初始化期间函数调用的顺序是确定性的吗?

qt - 页面呈现/加载所有组件时的事件

qt - Qt Quick Controls 2 上 SpinBox 的后缀选项

Qt5.8 QML 为什么只读的 Controls2.TextArea 有 ibeam 光标?

c++ - 如何在 C++ 中从 QString 传递 TextField 文本?

c++ - 如何在 C++ 端获取 QQuickItem 的有效实例