【问题标题】:How to indent paragraph in QtextEdit如何在 QtextEdit 中缩进段落
【发布时间】:2022-08-13 22:23:25
【问题描述】:

我希望文本像 Word 中一样缩进。代码不起作用: self.textEdit.setStyleSheet(\"QTextEdit {text-indent: 60px}\")

    标签: qt text


    【解决方案1】:

    使用QTextBlockFormat.setTextIndentQTextCursor.mergeBlockFormat 而不是QTextEdit.setStyleSheet

        cursor = QTextCursor(self.textEdit.document())
        cursor.select(QTextCursor.Document)
        fmt = QTextBlockFormat()
        fmt.setTextIndent(60)
        cursor.mergeBlockFormat(fmt)
    

    结果:

    UPD正如@ВалерияГригорьева 正确指出的那样,在粘贴纯文本时会丢弃缩进(尽管我希望默认实现应该从当前的block 中获取缩进)。因此我们需要覆盖QTextEdit.insertFromMimeData 并在插入时应用缩进:

    cursor = self.textCursor()
    fmt = QTextBlockFormat()
    fmt.setTextIndent(cursor.blockFormat().textIndent())
    cursor.mergeBlockFormat(fmt)
    cursor.insertText(md.text())
    

    另一方面,对于粘贴富文本,我们不想抑制缩进,因此我们可以依赖QTextEdit.insertFromMimeData 的默认实现。

    完整的代码示例(PyQt5):

    import sys
    from PyQt5.QtWidgets import QTextEdit, QApplication
    from PyQt5.QtGui import QTextDocument, QTextCursor, QTextBlockFormat
    
    
    class TextEditor(QTextEdit):
    
        def __init__(self):
            super().__init__()
            self.initUI()
    
        def initUI(self):
            # format text edit
            self.setPlainText(
                    "Impedit voluptatem sequi quae quo quos. \n" + 
                    "Asperiores non repellat culpa nihil. \n" + 
                    "Voluptatum ut numquam dolorem molestiae voluptatem " + 
                    "est modi necessitatibus. \n" + 
                    "Hic rerum voluptas voluptatem. \n" + 
                    "Ut expedita unde eum molestias voluptatem aut" + 
                    "dignissimos dolor. \n" + 
                    "Non illo dolore ut doloremque ut.")
            cursor = QTextCursor(self.document())
            cursor.select(QTextCursor.Document)
            fmt = QTextBlockFormat()
            fmt.setTextIndent(60)
            cursor.mergeBlockFormat(fmt)
            # setup UI
            self.setGeometry(300, 300, 300, 200)
            self.setWindowTitle('Text indent')
            self.show()
    
        def insertFromMimeData(self, md):
            # leave processing of the rich text as it is
            if md.hasFormat("application/x-qrichtext") or md.hasHtml():
                super().insertFromMimeData(md)
            else:
                # force indentation from the current block 
                # (shouldn't Qt do this by default, huh?)
                cursor = self.textCursor()
                fmt = QTextBlockFormat()
                fmt.setTextIndent(cursor.blockFormat().textIndent())
                cursor.mergeBlockFormat(fmt)
                cursor.insertText(md.text())
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        ex = TextEditor()
        sys.exit(app.exec())
    

    【讨论】:

    • 谢谢,但你能显示你程序的所有代码吗,因为我收到了一些错误
    • @ВалерияГригорьева 更新了完整的代码示例
    • 非常感谢。但我有一个新问题 :) 这仅适用于手动文本输入,如果我将文本粘贴到我的小部件中,格式将不再起作用。
    • @ВалерияГригорьева 看到我的更新
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    相关资源
    最近更新 更多