Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt 为什么是文本';s宽度/高度和锚固件在QML中冲突?_Qt_Qml_Qtquick2 - Fatal编程技术网

Qt 为什么是文本';s宽度/高度和锚固件在QML中冲突?

Qt 为什么是文本';s宽度/高度和锚固件在QML中冲突?,qt,qml,qtquick2,Qt,Qml,Qtquick2,我想指定文本的宽度和高度,以便fontSizeMode:Text.Fit可以工作。但在那之后,文本的锚就不起作用了。这怎么会发生 Rectangle { width: 360; height: 360 Rectangle { width: parent.width / 3; height: parent.height / 6 anchors.centerIn: parent border{ color: "red"; width:

我想指定文本的宽度和高度,以便fontSizeMode:Text.Fit可以工作。但在那之后,文本的锚就不起作用了。这怎么会发生

Rectangle {
    width: 360; height: 360
    Rectangle {
        width: parent.width / 3; height: parent.height / 6
        anchors.centerIn: parent
        border{ color: "red"; width: 5 }
        Text {
            anchors {
                centerIn: parent
//                verticalCenter: parent.verticalCenter
//                horizontalCenter: parent.horizontalCenter
            }

            width: parent.width
            height: parent.height
            font.pixelSize: 50
            fontSizeMode: Text.Fit
            text: "Hello, world!"
        }
    }
}

嗯,他们工作得很好。但由于您设置了宽度和高度,
centerrin
anchor不会更改任何内容,因为文本已经位于父对象的中间。您应该设置对齐,以获得适当的效果:

Text {
    anchors.fill: parent
    font.pixelSize: 100
    fontSizeMode: Text.Fit
    horizontalAlignment: Text.AlignHCenter
    verticalAlignment: Text.AlignVCenter
    text: "Hello, world!"
}