QML TextArea奇怪的填充

QML TextArea strange padding

我有一个 TextArea。如果我设置填充不断填充正常工作。

import QtQuick 2.9
import QtQuick.Controls 2.2

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

    TextArea{
        font.pixelSize: 20
        anchors.fill: parent
        wrapMode: TextArea.Wrap
        leftPadding: 100 //*parent.width/640
        rightPadding: 100 //*parent.width/640
    }
}

如果我取消注释上面的行,那么我会出现奇怪的行为。 我做错了什么?

截图

这似乎是一个错误,可能是缺少一些更新,在设置 ApplicationWindowcontentItem 的宽度时,所以行长计算不正确。

如果你写:

leftPadding: {
    console.log(parent, parent.width)
    return 100 * parent.width/640
}

你可以看到,parent.width最初设置为0,然后变为640。当发生这种变化时,信号肯定有问题。

调整 window 的大小将更新行的长度,因此恢复了正确的布局。您可以尝试在 http://bugreports.qt.io 上提交错误报告以修复它。

除此之外,您可以给您的 ApplicationWindow 一个 ID 并使用它来代替 parent

import QtQuick 2.7
import QtQuick.Controls 2.0

ApplicationWindow {
    id: win
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    TextArea{
        font.pixelSize: 20
        anchors.fill: parent
        wrapMode: TextArea.Wrap
        leftPadding: 100 * win.width/640
        rightPadding: 100 * win.width/640
    }
}