0

I have a QLabel with an unknown text that must be right-to-left, no matter which language is it. 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. But I want it to be RtL no matter which language is it.
How to fix this problem?

edit: I added a picture to describe the problem enter image description here

mhn2
  • 119
  • 9

1 Answers1

1

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.

widget.setAlignment(Qt.AlignRight | Qt.AlignAbsolute)
alec
  • 5,799
  • 1
  • 7
  • 20