pythonpyqt5farsiqplaintextedit

How to align QPlainTextEdit to right?


I want to write a notepad in python with PyQt5 for persian language. how can I align the thext in QPlainTextEdit to right? this is my code:

from PyQt5.QtWidgets import QApplication, QMainWindow, QPlainTextEdit
from PyQt5.QtCore import Qt, QLocale

class TextBox(QPlainTextEdit):
    def persian(self):
        self.setFixedSize(640, 480)

        self.setLayoutDirection(Qt.RightToLeft)
        self.setLocale(QLocale(QLocale.Persian, QLocale.Iran))


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.GUI()

    def GUI(self):
        self.setWindowTitle("My title")
        self.setFixedSize(640, 480)

        self.text = TextBox(self)
        self.text.persian()


app = QApplication([])
window = MainWindow()
window.show()

app.exec_()

Solution

  • You can use a QTextEdit instead of QPlainTextEdit and use setAlignment(Qt.AlignRight), e.g.

    from PyQt5.QtWidgets import QTextEdit
    from PyQt5.QtCore import Qt
    
    class TextBox(QTextEdit):
        def persian(self):
            self.setFixedSize(640, 480)
    
            self.setLayoutDirection(Qt.RightToLeft)
            self.setLocale(QLocale(QLocale.Persian, QLocale.Iran))
            # set text alignment to AlignRight
            self.setAlignment(Qt.AlignRight)