如何使Pyside6中plaintextedit中的每个段落都有缩进?

发布于 2025-01-15 11:37:55 字数 970 浏览 7 评论 0原文

我希望 plainTextEdit.text 中的每个段落都有文本缩进。
我尝试使用 setTextIndent()。但没有成功。
这是我的代码

from ui2 import Ui_Form
from PySide6.QtWidgets import QApplication,QWidget
from PySide6 import QtCore,QtGui
from PySide6.QtGui import QTextCursor


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)

        doc=self.ui.plainTextEdit.document()
        a=doc.firstBlock().blockFormat()
        a.setTextIndent(100)
        cursor = QTextCursor(doc)
        cursor.setBlockFormat(a)
        cursor.insertText("This is the first paragraph\nThis is the second paragraph")
        print(self.ui.plainTextEdit.document().toPlainText())


app = QApplication([])
mainw = MainWindow()
mainw.show()
app.exec()

这是我的打印:

This is the first paragraph
This is the second paragraph

没有文本缩进。

I want every paragraph in plainTextEdit.text has text-indent.
I tried to use setTextIndent(). But it didn't work.
This is my code

from ui2 import Ui_Form
from PySide6.QtWidgets import QApplication,QWidget
from PySide6 import QtCore,QtGui
from PySide6.QtGui import QTextCursor


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)

        doc=self.ui.plainTextEdit.document()
        a=doc.firstBlock().blockFormat()
        a.setTextIndent(100)
        cursor = QTextCursor(doc)
        cursor.setBlockFormat(a)
        cursor.insertText("This is the first paragraph\nThis is the second paragraph")
        print(self.ui.plainTextEdit.document().toPlainText())


app = QApplication([])
mainw = MainWindow()
mainw.show()
app.exec()

This is my print:

This is the first paragraph
This is the second paragraph

which don't have textindent.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何止钟意 2025-01-22 11:37:55

您必须使用 QTextEdit:

from PySide6.QtWidgets import QApplication, QTextEdit
from PySide6.QtGui import QTextCursor

app = QApplication([])

te = QTextEdit()
te.resize(640, 480)
te.show()

cursor = QTextCursor(te.document())
block_format = cursor.blockFormat()
block_format.setTextIndent(100)
cursor.setBlockFormat(block_format)
cursor.insertText("This is the first paragraph\nThis is the second paragraph")

app.exec()

在此处输入图像描述

You have to use QTextEdit:

from PySide6.QtWidgets import QApplication, QTextEdit
from PySide6.QtGui import QTextCursor

app = QApplication([])

te = QTextEdit()
te.resize(640, 480)
te.show()

cursor = QTextCursor(te.document())
block_format = cursor.blockFormat()
block_format.setTextIndent(100)
cursor.setBlockFormat(block_format)
cursor.insertText("This is the first paragraph\nThis is the second paragraph")

app.exec()

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文