使用 fontSizeMode 根据 qml 中的文本长度或大小自动调整文本的像素大小

Autosizing the pixel size of text based on text length or size in qml using fontSizeMode

我正在尝试在 qml 中使用 fontSizeMode 以修复矩形中的大文本。

import QtQuick 2.9
import QtQuick.Window 2.2

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    Rectangle
    {
    color: "Red"
    height:50
    width:50

    Text { text: "Hello"; fontSizeMode: Text.Fit; minimumPixelSize: 5; font.pixelSize: 50 }
}
}

我的目标是在文本大于矩形时缩小文本大小,并且如程序所示给出了最小像素大小。 但文字并没有缩小。我该如何解决这个问题?

得到答案我应该使用 width:parent.width height:parent.height 使其工作

工作代码:

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    Rectangle
    {
    color: "Red"
    height:50
    width:50

    Text { 
width:parent.width
 height:parent.height
text: "Hello"; fontSizeMode: Text.Fit; minimumPixelSize: 5; font.pixelSize: 50 }
}