如何在QLineEdit/QLabel等中从左到右显示阿拉伯符号? [英] How to display Arabic notations in left-to-right direction in QLineEdit/QLabel etc.?
问题描述
在Qt的实现中,阿拉伯符号以从右到左的方向显示,因此任何包含阿拉伯符号的字符串都将右对齐.
In Qt's implementation arabic notation is shown in right-to-left direction, thus any strings that contain arabic notations will be right-aligned.
但是我的应用程序想要做的是沿从左至右的方向显示所有文本,无论是否包含阿拉伯符号.并且所有文本都左对齐.
But what my application wants to do is showing all texts in left-to-right direction, whether it contains arabic notations or not. And all texts are left-aligned.
示例如下:
-
这是 我要实现的
This is what I want to implement
这是 QLineEdit
以默认方式显示包含阿拉伯符号的文本的方式
This is how QLineEdit
displays texts containing arabic notations in its default way
QLabel
就是这样
This is how QLabel
does it
在此处粘贴测试字符串.劈抠门哥劈(
Paste the test string here. ە抠门哥ە(
提供替代解决方案.
最后,我可以使用具有QTextDocument
的QTextEdit
大致实现我的目标.以下代码段显示了我是如何做到的.但是我不知道Qt如何从全局角度处理文本方向,因此我无法通过QLabel
等实现我的目标.
Finally I can achieve my goal roughly by using QTextEdit
which has a QTextDocument
. The following code snippet shows how I did it. But I have no idea how Qt deals with text direction from a global perspective, so I can't achieve my goal with QLabel
etc... It would be great if someone can give some detailed information about Qt's text engine.
QTextDocument *doc = ui->textEdit->document();
QTextOption textOption = doc->defaultTextOption();
textOption.setTextDirection(Qt::LeftToRight);
doc->setDefaultTextOption(textOption);
ui->textEdit->setDocument(doc);
推荐答案
Unicode提供方向格式字符,Qt很好地支持它.
Unicode provides Directional Formatting Characters,and Qt supports it well.
因此,对于 QLabel
和 QLineEdit
等,我们可以插入 LRM
控制字符
(在 Unicode双向算法中定义),该字符串在 RightToLeft 字符串的开头,以使字符串 left-alignment .有关 Unicode双向算法,请点击此处.
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;
对于具有 QTextDocument
的 QTextEdit
等,我们可以使 RightToLeft 字符串左对齐通过将 QTextDocment
的textDirection
设置为 Qt::LeftToRight
来实现strong>.
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
具有成员功能 isRightToLeft
来确定字符串是否为 RightToLeft .例如,以从右至左书写语言表示的字符串开头是 RightToLeft .
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.
这篇关于如何在QLineEdit/QLabel等中从左到右显示阿拉伯符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!