pyqt5-QTextEdit

1、介绍

这是pyqt的多行文本输入组件,支持html、markdown的修饰样式。

2、类和初始化

class QTextEdit(QAbstractScrollArea):
    """
    QTextEdit(parent: QWidget = None)
    QTextEdit(str, parent: QWidget = None)
    """
    def __init__(self, *__args):
        pass
  • 继承关系:QTextEdit->QAbstractScrollArea->QFrame->QWidget

示例:

qtextedit = QTextEdit(self.w)

3、属性

    AutoAll = -1
    AutoBulletList = 1
    AutoNone = 0
    FixedColumnWidth = 3
    FixedPixelWidth = 2
    NoWrap = 0
    WidgetWidth = 1

4、方法

setHtml(self, str)
setMarkdown(self, str)
setText(self, str) 
setPlainText(self, str)
setPlaceholderText(self, str)
append(self, str)
insertHtml(self, str)
insertPlainText(self, str)
placeholderText(self) -> str 
toHtml(self) -> str 
toMarkdown(self, features: Union[QTextDocument.MarkdownFeatures, QTextDocument.MarkdownFeature] = QTextDocument.MarkdownDialectGitHub) -> str
toPlainText(self) -> str

selectAll(self)

5、事件

selectionChanged(self) [signal]
textChanged(self) [signal]

6、示例

text_cursor = self.qtextedit.textCursor()
  • 创建光标对象
tcf = QTextCharFormat()  # 创建一个格式对象
tcf.setToolTip('pyqt5中文网')  # 设置提示文本
tcf.setFontFamily('楷书')  # 设置字体
tcf.setFontPointSize(25)  # 设置字体大小
# 2.通过光标插入文本,传入上面的格式对象
text_cursor.insertText('www.PyQt5.cn', tcf)
  • 通过光标对象插入文本
tcf = QTextImageFormat()
tcf.setName('123.jpg')
tcf.setWidth(40)
tcf.setHeight(40)
text_cursor.insertImage(tcf)
  • 通过光标插入图片
tcf = QTextDocumentFragment().fromHtml("<a href='https://www.pyqt5.cn'>PyQt5中文网</a>") # 插入富文本
# tcf.fromHtml("<a href='https://www.pyqt5.cn'>PyQt5中文网</a>") # fromHtml有返回值,所以必须给个变量名,这样写就错了
# tcf = QTextDocumentFragment().fromPlainText("<a href='https://www.pyqt5.cn'>PyQt5中文网</a>") # 插入普通文本
text_cursor.insertFragment(tcf)
  • 通过光标插入文件
posted @ 2023-04-16 23:40  挖洞404  阅读(181)  评论(0编辑  收藏  举报