Home > Mobile >  align right-to-left text with QPainter::drawText
align right-to-left text with QPainter::drawText

Time:08-21

I am trying to paint a right-to-left text with QPainter. It however still aligns the text to left despite the fact that it should be right-aligned. Or at least it is right-aligned when displayed in QTextEdit. That am I doing wrong? See the example:

QTextOption option;
option.setTextDirection(Qt::LayoutDirectionAuto);
painter->drawText(rect, "خامل\nخاملخامل", option); // this is just testing text, I have no idea what it means, hopefully it is not something offensive :)

This is what I get. The red arrow shows where it should be aligned.

enter image description here

Of course I cannot use fixed right alignment because I do not whether the text in question is left-to-right or right-to-left text. So it must work for both directions automatically depending on the text.

UPDATE: I want to emphasize that I need a solution which automatically recognizes the language/writing system of the text (left-to-right, such as English, versus right-to-left, such as Arabic) and aligns the text automatically.

CodePudding user response:

you should check your text with isRightToLeft() function.

QString enter image description here

  •  Tags:  
  • c qt
  • Related