gpt4 book ai didi

python - QPlainTextEdit 是 RightToLeft 但显示 LeftToRight

转载 作者:行者123 更新时间:2023-12-01 01:02:09 29 4
gpt4 key购买 nike

我使用QPlainTextEdit创建了一个 View 并设置了setLayoutDirection(QtCore.Qt.RightToLeft)。 self.plaintxt.isRightToLeft() 的输出为 1,但在纯 TextView 中,波斯语和英语文本从左侧显示。我的代码发生了什么?

代码:

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

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

关于python - QPlainTextEdit 是 RightToLeft 但显示 LeftToRight,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55702871/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com