【问题标题】:Justifying multi-line text in QML Text element在 QML 文本元素中对齐多行文本
【发布时间】:2015-03-11 09:37:31
【问题描述】:

我无法在 QML(Windows MSVC2010 OpenGL 下的 Qt 5.2.1)中获取文本。下面是示例代码:

import QtQuick 2.2
import QtQuick.Window 2.1

Window {
    visible: true
    width: 160
    height: 60

    Text {
        text: qsTr("Hello\nWorld World2 World3")
        horizontalAlignment: Text.AlignRight
        anchors.centerIn: parent
    }
}

这是它在 Qt Creator 的设计器中的外观:

这是在 Qt Creator 的新项目向导生成的“Qt Quick Application”下运行时的外观(仅在 .qml 文件中进行更改,如上所示):

如何让它在应用程序中看起来正确?

这是QTBUG-30896(感谢@Meefte 提供链接),已在 Qt 5.3 中修复。 Qt 5.2.1 的任何合理解决方法(因为在这种情况下升级 Qt 版本有“外部”困难)?

【问题讨论】:

  • 看起来像QTBUG-30896。它已在 Qt 5.3.0 中修复

标签: qt qml qt5 text-alignment qt5.2


【解决方案1】:

作为一个错误修复,您可以为文本元素提供固定宽度:

import QtQuick 2.2
import QtQuick.Window 2.1

Window {
    visible: true
    width: 160
    height: 60

    Text {
        width: 160
        text: qsTr("Hello\nWorld World2 World3")
        horizontalAlignment: Text.AlignRight
        anchors.centerIn: parent
    }
}

【讨论】:

    【解决方案2】:

    一些丑陋的解决方法,但至少它可以满足您的需求:)

    ColumnLayout {
        anchors.centerIn: parent
        Repeater {
            id: repeater
            model: "Hello\nWorld World2 World3".split(/[\r\n]/g);
            Text {
                text: repeater.model[index]
                horizontalAlignment: Text.AlignRight
                Layout.fillWidth: true
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 2019-06-22
      • 2012-09-14
      • 1970-01-01
      相关资源
      最近更新 更多