说明
QTextEdit是一个高级的WYSIWYG(What You See Is What You Get所见即所得)编辑/查看器,支持使用HTML4标签子集的富文本格式。可以处理大型文档并快速响应用户的输入,可以加载纯文本和富文本文件,用来显示图像、列表和表格。QTextEdit的父类是QAbstractScrollArea,可以通过滚动条调整显示界面。
常用信号
信号说明
QTextEdit.textChanged()文本内容发生改变时发射QTextEdit.selectionChanged()选中文本内容发生改变时发射QTextEdit.cursorPositionChanged()光标位置发生改变时发射QTextEdit.currentCharFormatChanged(QTextCharFormat)当前字符格式发生改变时发射(带有参数)QTextEdit.copyAvailable(bool)复制可用时(返回布尔量)QTextEdit.redoAvailable(bool)重做可用时(返回布尔量)QTextEdit.undoAvailable(bool)撤销可用时(返回布尔量)
测试Demo
import sys
from PyQt5
.QtWidgets
import QApplication
, QWidget
from PyQt5
.QtWidgets
import QTextEdit
, QPushButton
, QVBoxLayout
class QTextEditDemo(QWidget
) :
def __init__(self
):
super(QTextEditDemo
,self
).__init__
()
self
.initUI
()
def initUI(self
):
self
.setWindowTitle
('QTextEdit控件')
self
.resize
(500, 300)
self
.textEdit
= QTextEdit
()
self
.btnText
= QPushButton
('显示文本')
self
.btnHTML
= QPushButton
('显示HTML')
self
.btnToText
= QPushButton
('获取文本')
self
.btnToHTML
= QPushButton
('获取HTML')
layout
= QVBoxLayout
()
layout
.addWidget
(self
.textEdit
)
layout
.addWidget
(self
.btnText
)
layout
.addWidget
(self
.btnToText
)
layout
.addWidget
(self
.btnHTML
)
layout
.addWidget
(self
.btnToHTML
)
self
.setLayout
(layout
)
self
.btnText
.clicked
.connect
(self
.onClick_BtnText
)
self
.btnHTML
.clicked
.connect
(self
.onClick_BtnHTML
)
self
.btnToText
.clicked
.connect
(self
.onClick_BtnToText
)
self
.btnToHTML
.clicked
.connect
(self
.onClick_BtnToHTML
)
def onClick_BtnText(self
):
self
.textEdit
.setPlainText
('这是文本输入框控件Demo')
def onClick_BtnToText(self
):
print(self
.textEdit
.toPlainText
())
def onClick_BtnHTML(self
):
self
.textEdit
.setHtml
('<font color="blue" size="5">Hello World</font>')
def onClick_BtnToHTML(self
):
print(self
.textEdit
.toHtml
())
if __name__
== '__main__':
app
= QApplication
(sys
.argv
)
main
= QTextEditDemo
()
main
.show
()
sys
.exit
(app
.exec_
())
运行效果