bardao
bardao

Reputation: 954

How to set the font size of a list header to a consistent value

I am trying to set the header of a ListView to a consistent font sizes. Given that I also need it to automatically grow based on the height and width of the container, how can I achieve a good-looking header row?

What I tried already is the below. One way of solving this is not to set the width of each Text the same way and give different preferredWidth to each based on the string length.

BUT, what if my string is dynamically loaded, and I don't know how long my string would be. What is a good way of achieving a good-looking header instead of the one I have.

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
        }
    }
}

enter image description here

Upvotes: 0

Views: 411

Answers (0)

Related Questions