首页 > 解决方案 > QML如何将列表标题的字体大小设置为一致的值

问题描述

我正在尝试将headera设置ListView为一致的font大小。鉴于我还需要它根据容器的height和自动增长width,我怎样才能获得一个好看的header行?我已经尝试过的是以下内容。解决此问题的一种方法是不要以Text相同的方式设置每个的宽度,并preferredWidth根据字符串长度为每个设置不同的宽度。但是,如果我的字符串是动态加载的,而我不知道我的字符串会有多长怎么办。什么是获得漂亮标题而不是我拥有的标题的好方法。谢谢。

import QtQuick 2.4
import QtQuick.Window 2.2
import QtQuick.Layouts 1.2

Window {
    visible: true
    width: 400
    height: 200

    RowLayout {
        anchors.fill: parent
        Text {
            text: qsTr("Short")
            Layout.fillHeight: true
            Layout.fillWidth: true
            Layout.preferredWidth: 1
            fontSizeMode: Text.HorizontalFit | Text.VerticalFit
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
            minimumPointSize: 12
            font.pointSize: 40

        }
        Text {
            text: qsTr("Medium String")
            Layout.fillHeight: true
            Layout.fillWidth: true
            Layout.preferredWidth: 1
            fontSizeMode: Text.HorizontalFit | Text.VerticalFit
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
            minimumPointSize: 12
            font.pointSize: 40
        }
        Text {
            text: qsTr("A much longer String")
            Layout.fillHeight: true
            Layout.fillWidth: true
            Layout.preferredWidth: 1
            fontSizeMode: Text.HorizontalFit | Text.VerticalFit
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
            minimumPointSize: 12
            font.pointSize: 40
        }
        Text {
            text: qsTr("An absurdly and unecessary long String!!")
            Layout.fillHeight: true
            Layout.fillWidth: true
            Layout.preferredWidth: 1
            fontSizeMode: Text.HorizontalFit | Text.VerticalFit
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
            minimumPointSize: 12
            font.pointSize: 40
        }
    }

}

在此处输入图像描述

标签: qtqmlqtquick2

解决方案


推荐阅读