mng97
mng97

Reputation: 85

QPlainTextEdit is RightToLeft but displays LeftToRight

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_())

Upvotes: 3

Views: 1386

Answers (2)

elcuco
elcuco

Reputation: 9208

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.

Upvotes: 2

ncica
ncica

Reputation: 7206

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

Upvotes: 5

Related Questions