How to display Arabic notations in left-to-right direction in QLineEdit/QLabel etc.?

不羁岁月 提交于 2019-12-03 21:40:52
waterd

Unicode provides Directional Formatting Characters,and Qt supports it well.

Thus,for QLabel and QLineEdit etc. we can insert a LRM control character
,which is define in Unicode Bidirectional Algorithm, at the beginning of a RightToLeft string to make the string left-alignment.For more information about Unicode Bidirectional Algorithm,click here.

QString(QChar(0x200E))+strText;

And for QTextEdit etc. which has a QTextDocument we can make RightToLeft string left-alignment by setting QTextDocment's textDirection to Qt::LeftToRight.

ps:
QString has a isRightToLeft member function to decide whether the string is RightToLeft or not. For example,a string that begins with a notation from Right-to-left writting language is RightToLeft.

I answered another one,which maybe helpful for finding your own solution.

In Qt documentation about setLayoutDirection you can read :

This method no longer affects text layout direction since Qt 4.7.

So you can not use this method. For QLineEdit you can send a Qt::Key_Direction_L keyboard event to the line edit to make it left to right event if the characters are Arabic or Persian :

QKeyEvent event(QEvent::KeyPress, Qt::Key_Direction_L, 0);
qApp->sendEvent(ui->lineEdit, &event);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!