Set text direction for QLabel?
Asked Answered
P

1

2

Is there a way to set the text direction for a QLabel? I've got a situation in which I have QLabel objects whose text is only punctuation, and I want that to be displayed either in RTL or LTR format. (For instance, parentheses or quotation marks need to reverse depending on the text direction.) I've tried calling QLabel::setLayoutDirection, but to no effect.

#include <QApplication>
#include <QLabel>

int main(int argc, char **argv)
{
    QApplication app (argc, argv);
    QLabel label(" :  «");
    label.setFont( QFont("Times New Roman", 72) );
    label.setLayoutDirection( Qt::RightToLeft );
    // label.setLayoutDirection( Qt::LeftToRight );
    label.show();
    return app.exec();
}

A workaround at this point is to prepend 0x202E ("Right-to-Left Override") to the string, but that's obviously a bit clunky.

Personal answered 15/10, 2014 at 9:21 Comment(4)
You need to set text to right or to automatically transform a label with the text abcd to dcba?Marchpast
@luliu your example is incorrect since abcd has well defined text direction.Mori
did you tried change locale of widget?Mori
Thanks for the suggestion. I tried the following with no result: label.setLocale(QLocale(QLocale::Arabic, QLocale::Egypt));Personal
M
1
label.setAlignment(Qt::AlignRight);
Marchpast answered 15/10, 2014 at 9:34 Comment(1)
setAlignment() was the correct answer for me! ... as setLayoutDirection() does not work at all.Algid

© 2022 - 2024 — McMap. All rights reserved.