C

Qt Quick Ultralite font_bindings Example

// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial
import QtQuick 2.15
import QtQuick.Controls 2.15

Rectangle {
    // The Fontmap used by this example has defined the following
    // font class names:
    //
    // "Roboto"
    // "Roboto Light"
    // "Roboto Italic"
    //
    // QUL_DEFAULT_FONT_FAMILY is set to "Roboto"

    Flickable {
        anchors.fill: parent
        contentHeight: appLayout.height
        contentWidth: appLayout.width
        interactive: true

        Column {
            id: appLayout

            Switch {
                id: italicSwitch
                text: "Italic style"
            }

            Slider {
                id: pixelSizeSlider
                from: 24
                to: 100
            }

            Text {
                text: "Hello world"
                // Depending on the 'italicSwitch.checked' value, the font class
                // name can be "Roboto" or "Roboto Italic".
                font.italic: italicSwitch.checked
                font.pixelSize: pixelSizeSlider.value
            }

            Text {
                text: "from Qt."
                // An alternativele way is to provide a full class name via font.family property.
                font.family: italicSwitch.checked ? "Roboto Italic" : "Roboto"
                font.pixelSize: pixelSizeSlider.value
            }

            Switch {
                id: lightSwitch
                text: "Extra light style"
            }

            Text {
                id: lightText
                text: "Hello again world"
                font.pixelSize: pixelSizeSlider.value
                // Depending on the 'lightSwitch.checked' value, the font class
                // name can be "Roboto" or "Roboto Light".
                font.weight: lightSwitch.checked ? Font.Light : Font.Normal
            }

            Text {
                // To save the memory resources, the font configurations can be shared
                // between elements. This happens automatically in the emitted cpp code
                // when font configuration uses only constant values in font.* bindings.
                font: lightText.font
                text: "from Qt."
            }

            Switch {
                id: qualitySwitch
                text: "High quality"
                checked: true
            }

            Text {
                id: qualityText
                text: "Variable glyph quality"
                font: Qt.font({
                    pixelSize: pixelSizeSlider.value,
                    // Depending on the 'qualitySwitch.checked' value, the font
                    // rendering quality changes.
                    quality: qualitySwitch.checked ? Font.QualityVeryHigh : Font.QualityVeryLow
                })
            }
        }
    }
}