我使用QPlainTextEdit
进行了查看并设置了setLayoutDirection(QtCore.Qt.RightToLeft)
。 self.plaintxt.isRightToLeft()
的输出为1,但在纯文本视图中,波斯语和英语文本从左开始显示。我的代码会怎样?
码:
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_())
最佳答案
QWidget.setLayoutDirection不再影响QTextEdit,QLineEdit和基于它们的小部件的文本布局方向(Qt.LeftToRight或Qt.RightToLeft)。
要以编程方式强制文本方向,可以使用具有不同textDirection属性的新QTextOption来更改与该小部件关联的QTextDocument的defaultTextOption。
QTextDocument * QPlainTextEdit :: document()const
返回一个指向
基础文档。
无效的QTextDocument :: setDefaultTextOption(const QTextOption&option)
将默认文本选项设置为option。
self.page_view.document().setDefaultTextOption(QTextOption(Qt.AlignRight))
关于python - QPlainTextEdit为RightToLeft,但显示LeftToRight,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55702871/