Qt 基于容器的Qml文本大小

Qt 基于容器的Qml文本大小,qt,text,label,qml,Qt,Text,Label,Qml,我正在使用QtQuick开发一个应用程序(Qt5.8)。我的文本显示在多个位置,因此我创建了一个组件用于显示文本。文本区域的位置和大小可能有所不同。如果我只想显示以下内容,如何调整文本,使数据水平显示在一行中,且所有文本大小相同 襟翼1 降档 微调-1.0 我使用文本,并且能够接近,但是由于齿轮减速有更多的字符,文本比襟翼和修剪小。所以我开始使用标签。有人能更好地了解如何根据容器的宽度或高度来确定文本大小吗 main.qml import QtQuick 2.6 import QtQuick.W

我正在使用QtQuick开发一个应用程序(Qt5.8)。我的文本显示在多个位置,因此我创建了一个组件用于显示文本。文本区域的位置和大小可能有所不同。如果我只想显示以下内容,如何调整文本,使数据水平显示在一行中,且所有文本大小相同

襟翼1
降档
微调-1.0

我使用文本,并且能够接近,但是由于齿轮减速有更多的字符,文本比襟翼和修剪小。所以我开始使用标签。有人能更好地了解如何根据容器的宽度或高度来确定文本大小吗

main.qml

import QtQuick 2.6
import QtQuick.Window 2.2

Window {
    id: windowRoot
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Rectangle{
        height: windowRoot.height * .80
        width: windowRoot.width * .80
        color: "green"
        anchors.centerIn: parent
        Rectangle {
            id: rect1
            opacity: .5
            anchors.top: parent.top
            anchors.left: parent.left
            color: "lime"
            border.color: "orange"
            height: parent.height * 1/2
            width: parent.width * 1/2
            TextHolder {
                anchors.top: parent.top
                anchors.left: parent.left
                width: parent.width * 1/4
                height: parent.height
            }
        }
        Rectangle {
            id: rect2
            anchors.top: parent.top
            anchors.left: rect1.right
            color: "yellow"
            border.color: "blue"
            height: parent.height * 1/2
            width: parent.width * 1/2
        }
        Rectangle {
            id: rect3
            anchors.top: rect1.bottom
            anchors.left: parent.left
            color: "pink"
            border.color: "red"
            height: parent.height * 1/2
            width: parent.width * 1/2
            TextHolder {
                anchors.top: parent.top
                anchors.left: parent.left
                width: parent.width * 1/4
                height: parent.height * 1/2
            }
        }
        Rectangle {
            id: rect4
            anchors.top: rect2.bottom
            anchors.left: rect3.right
            color: "orange"
            border.color: "lime"
            height: parent.height * 1/2
            width: parent.width * 1/2
            TextHolder {
                anchors.bottom: parent.bottom
                height: parent.height * 1/4
                width: parent.width * 1/4
                anchors.horizontalCenter: parent.horizontalCenter
            }
        }
    }
}
TextHolder.qml

import QtQuick 2.0
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.1

Rectangle {
    color: "purple"
    border.color: "steelblue"
    border.width: 3
    property color colorOfText: "white"
    property real textSize: 48
    Item {
        id: inputs
        property int flapHndl: 1
        property int gearHndl: 1
        property real trim: -1.0
    }

    clip: true

    ColumnLayout {
        anchors.top: parent.top
        width: parent.width
        height: parent.height
        spacing: 5

        Label {
            id: flapLabel
            text: "FLAPS " + inputs.flapHndl
            color: colorOfText
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            font.pixelSize: textSize
            fontSizeMode: Text.HorizontalFit
            Layout.fillWidth: true
        }

        Label {
            id: gearLabel
            text: {
                if (inputs.gearHndl === 1)
                    return "GEAR DOWN"
                else
                    return "GEAR UP"
            }
            color: colorOfText
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            font.pixelSize: textSize
            fontSizeMode: Text.HorizontalFit
            Layout.fillWidth: true
        }

        Label {
            id: trimLabel
            text: "TRIM " + inputs.trim.toFixed(1)
            color: colorOfText
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            font.pixelSize: textSize
            fontSizeMode: Text.HorizontalFit
            Layout.fillWidth: true
        }
    }
}
有人能更好地了解如何根据 它的容器的宽度还是高度

为了评估文本的宽度和高度,我们可以使用QML类型,并拥有我们可以尝试将文本放入的度量。如果文本仍然不合适,我们可以尝试调整字体大小。为此,可能需要使用JavaScript实现一些逻辑。因此,下面的代码是“反馈”类型解决方案的一个示例

Rectangle {
   property string textToShow
   property int widthLimit: 400

   onTextToShowChanged: {
      while (txtMeter.width > widthLimit) {
         txtMeter.font.pixelSize --;
      }
   }

   TextMetrics {
      id: txtMeter
      font.family: "Courier"
      font.pixelSize: 25
      text: textToShow
   }

   Text {
      font: txtMeter.font
      text: textToShow
   }
}
另外,这只是一个粗略的想法,您的实际实施可能会有所不同

有人能更好地了解如何根据 它的容器的宽度还是高度

为了评估文本的宽度和高度,我们可以使用QML类型,并拥有我们可以尝试将文本放入的度量。如果文本仍然不合适,我们可以尝试调整字体大小。为此,可能需要使用JavaScript实现一些逻辑。因此,下面的代码是“反馈”类型解决方案的一个示例

Rectangle {
   property string textToShow
   property int widthLimit: 400

   onTextToShowChanged: {
      while (txtMeter.width > widthLimit) {
         txtMeter.font.pixelSize --;
      }
   }

   TextMetrics {
      id: txtMeter
      font.family: "Courier"
      font.pixelSize: 25
      text: textToShow
   }

   Text {
      font: txtMeter.font
      text: textToShow
   }
}
另外,这只是一个粗略的想法,您的实际实现可能会有所不同

存在fontSizeMode文本属性

Text {
            id: goToParentFolderText
            anchors.fill: parent
            font.family: fontAwesomeSolid.name
            text: "\uf060"
            fontSizeMode: Text.Fit
            font.pointSize: 100
            color: Material.accent
}
fontSizeMode属性的Text

Text {
            id: goToParentFolderText
            anchors.fill: parent
            font.family: fontAwesomeSolid.name
            text: "\uf060"
            fontSizeMode: Text.Fit
            font.pointSize: 100
            color: Material.accent
}

我能够得到@Alexander V的工作答案,只是做了一点小改动。外部textToShow属性在更新TextMetrics块之前正在处理其更新(这导致宽度计算不正确)。您可以通过在TextMetrics块内触发onTextChanged来解决此问题

Rectangle {
   property string textToShow
   property int widthLimit: 400

   TextMetrics {
      id: txtMeter
      font.family: "Courier"
      font.pixelSize: 25
      text: textToShow

      onTextChanged: {
         while (txtMeter.width > widthLimit) {
            txtMeter.font.pixelSize --;
         }
      }
   }

   Text {
      font: txtMeter.font
      text: textToShow
   }
}

我能够得到@Alexander V的工作答案,只是做了一点小改动。外部textToShow属性在更新TextMetrics块之前正在处理其更新(这导致宽度计算不正确)。您可以通过在TextMetrics块内触发onTextChanged来解决此问题

Rectangle {
   property string textToShow
   property int widthLimit: 400

   TextMetrics {
      id: txtMeter
      font.family: "Courier"
      font.pixelSize: 25
      text: textToShow

      onTextChanged: {
         while (txtMeter.width > widthLimit) {
            txtMeter.font.pixelSize --;
         }
      }
   }

   Text {
      font: txtMeter.font
      text: textToShow
   }
}

对于使用此方法的任何人,我发现调用onTextToShowChanged后,txtMeter.width都可以更新。您可能需要将代码放在TextMetrics块内部名为onTextChanged的触发器中,以确保在必要的时间内更新内容order@KyleL再回答一个问题?对于使用此方法的任何人,我发现调用onTextToShowChanged后,txtMeter.width可以更新。您可能需要将代码放在TextMetrics块内部名为onTextChanged的触发器中,以确保在必要的时间内更新内容order@KyleL再回答一个?