2

I made a view with QPlainTextEdit and set setLayoutDirection(QtCore.Qt.RightToLeft). The output of self.plaintxt.isRightToLeft()is 1 but in the plain text view, persian and english text are displayed from left. What is happen in my code?

Code:

import sys, re

from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QLabel, QLineEdit, QMainWindow, QPushButton, QFileSystemModel, QTreeView, \
    QFileDialog, QComboBox, QPlainTextEdit
from PyQt5.QtCore import pyqtSlot


class App(QMainWindow):

    def __init__(self):
        super(App, self).__init__()

        self.title = 'by PyQt5 and python 3.7'
        self.left = 10
        self.top = 10
        self.width = 1000
        self.height = 500

        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        self.btn_browse = QPushButton('Browse', self)
        self.btn_browse.move(50, 20)
        self.btn_browse.clicked.connect(self.on_click)

        self.textbox = QLineEdit(self)
        self.textbox.move(170, 20)
        self.textbox.resize(280, 40)
        self.textbox.setAlignment(QtCore.Qt.AlignRight) # It is in right.

        self.page_view = QPlainTextEdit(self)
        self.page_view.move(20, 100)
        self.page_view.resize(800, 400)
        self.page_view.setLayoutDirection(QtCore.Qt.RightToLeft) # It is not in right.

        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())
Greg
  • 18,111
  • 5
  • 46
  • 68
mng97
  • 75
  • 9

2 Answers2

4

QWidget.setLayoutDirection no longer affects the text layout direction (Qt.LeftToRight or Qt.RightToLeft) of QTextEdit, QLineEdit and widgets based on them.

To programmatically force the text direction , you can change the defaultTextOption of the QTextDocument associated with that widget with a new QTextOption of different textDirection property.

QTextDocument *QPlainTextEdit::document() const

Returns a pointer to the underlying document.

void QTextDocument::setDefaultTextOption(const QTextOption &option)

Sets the default text option to option.

self.page_view.document().setDefaultTextOption(QTextOption(Qt.AlignRight))

enter image description here

ncica
  • 7,015
  • 1
  • 15
  • 37
  • Can you add a short description? – help-info.de Apr 16 '19 at 13:51
  • thanks, you know - code-only answers are considered low quality. – help-info.de Apr 16 '19 at 14:05
  • thanks for advice, I will be more detailed in the future :) @help-info.de – ncica Apr 16 '19 at 14:09
  • Are you sure that the correct approach to display RTL text is to use `Qt.AlightRight`? Alignment doesn't change the layout direction and will still cause BiDi text to be rendered incorrectly. Maybe you should use `Qt.RightToLeft` (https://doc.qt.io/qt-6/qt.html#LayoutDirection-enum)? – Guss Apr 06 '23 at 17:03
2

Don't use QPlainText, as it uses internally QPlainTextDocumentLayout which does not fully support RTL (automatic alignment for example - like you are looking for). You can use QTextEdit, or use a different document layout class (like QTextDocumentLayout) in your QPlainText.

The reason for those Plain classes exist, is to be faster by removing features... which you need.

elcuco
  • 8,948
  • 9
  • 47
  • 69