hyde
hyde

Reputation: 62777

Justifying multi-line text in QML Text element

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)?

Upvotes: 1

Views: 8666

Answers (2)

folibis
folibis

Reputation: 12854

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

Upvotes: 1

Mailerdaimon
Mailerdaimon

Reputation: 6080

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

Upvotes: 3

Related Questions