问题描述
在 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.
示例如下:
这就是我想要实现的
这就是 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
等实现我的目标... 如果有人能提供一些详细的有关 Qt 文本引擎的信息.
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 提供了Directional Formatting Characters,Qt 很好的支持.
Unicode provides Directional Formatting Characters,and Qt supports it well.
因此,对于 QLabel
和 QLineEdit
等,我们可以插入一个 LRM
控制字符
,在Unicode双向算法中定义,在RightToLeft字符串的开头,使字符串左对齐.有关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> string left-alignment 通过将 QTextDocment
的 textDirection
设置为 Qt::LeftToRight
.
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 等中以从左到右的方向显示阿拉伯符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!