1

I have trouble getting text justified in QML (Qt 5.2.1 under Windows MSVC2010 OpenGL). Here's example code:

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

Here's how it looks in Qt Creator's Designer:

Designer screenshot

And here's how it looks when Run under "Qt Quick Application" generated by Qt Creator's New Project Wizard (only change is in the .qml file, shown above):

enter image description here

How can I get it to look right in the application?

This is QTBUG-30896 (thanks @Meefte for link), fixed in Qt 5.3. Any reasonable workaround for Qt 5.2.1 (as upgrading the Qt version has "external" difficulties in this case)?

hyde
  • 60,639
  • 21
  • 115
  • 176
  • 1
    Looks like [QTBUG-30896](https://bugreports.qt.io/browse/QTBUG-30896). It is fixed in Qt 5.3.0 – Meefte Mar 11 '15 at 10:13

2 Answers2

3

As a bugfix you can give a fixed width to the text element:

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
    }
}
Mailerdaimon
  • 6,003
  • 3
  • 35
  • 46
1

Some ugly workaround but at least it does what you need :)

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
        }
    }
}
folibis
  • 12,048
  • 6
  • 54
  • 97