简体   繁体   English

PySide2 中的强制 RtL / PyQt5

[英]Forced RtL in PySide2 / PyQt5

I have a QLabel with an unknown text that must be right-to-left, no matter which language is it.我有一个带有未知文本的 QLabel,无论是哪种语言,它都必须是从右到左的。 So I wrote this code:所以我写了这段代码:

def add_label(self):
    text = self.text_input.text() # self.text_input is a QLineEdit
    widget = QLabel(text)
    widget.setAlignment(Qt.AlignRight)
    widget.setStyleSheet("color: #000000")
    self.layout.addWidget(widget) # self.layout is a QVBoxLayout
    # I can't setAlignment for self.layout because there is some other QLabels in there that have a LtR alignment.

The problem in here is when I write a Persian text in self.text_input and run the add_label function (via pressing a button) it will automatically change the alignment to RtL and when I call the widget.setAlignment(Qt.AlignRight) It will change back to LtR.这里的问题是当我在self.text_input中写一个波斯语文本并运行add_label function(通过按下一个按钮)它会自动将 alignment 更改为 RtL 当我调用widget.setAlignment(Qt.AlignRight)它会改变回到 LtR。 But I want it to be RtL no matter which language is it.但无论是哪种语言,我都希望它是 RtL。
How to fix this problem?如何解决这个问题?

edit: I added a picture to describe the problem编辑:我添加了一张图片来描述问题在此处输入图像描述

The solution would be to combine the alignment flag with Qt.AlignAbsolute so no matter the layout direction the text will always align to the right.解决方案是将 alignment 标志与Qt.AlignAbsolute结合使用,这样无论布局方向如何,文本都将始终向右对齐。

widget.setAlignment(Qt.AlignRight | Qt.AlignAbsolute)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM