QTextEdit是一个高级的WYSIWYG(What You See Is What You Get所见即所得)编辑/查看器,支持使用HTML4标签子集的富文本格式。
QTextEdit它经过优化,可以处理大型文档并快速响应用户的输入,可以加载纯文本和富文本文件,用来显示图像、列表和表格。
QTextEdit的父类是QAbstractScrollArea,可以通过滚动条调整显示界面。
文本内容设置
由于QTextEdit是支持普通文本和html标签的,分别有两种文本的操作
普通文本设定
QTextEdit.setPlainText(str) #普通文本设定 QTextEdit.insertPlainText(str) #光标处插入普通文本 QTextEdit.toPlainText() #普通文本获取
b.html标签文本设定,用法和普通文本一样。
QTextEdit.setHtml('str')
QTextEdit.insertHtml('str')
QTextEdit.toHtml()
b.html标签文本设定,用法和普通文本一样。
QTextEdit.setHtml('str')
QTextEdit.insertHtml('str')
QTextEdit.toHtml()
QTextEdit.append('str') #文本追加(不管光标位置)
QTextEdit.clear() #文本清除
为了演示,我们建立一个界面,界面上一个按钮btn,一个QTextEdit,btn对应了一个槽函数fun,为了演示不同的效果,在下面的代码中值改变fun函数,先把大的框架放出来
from PyQt5.Qt import *
import sys
app=QApplication(sys.argv)
window = QWidget()
te = QTextEdit(window)
window.resize(800,600)
btn = QPushButton('test',window)
btn.move(0,300)
def fun():
return None
btn.clicked.connect(fun)
window.show()
sys.exit(app.exec_())
插入文本
def fun():
tc = te.textCursor()
tcf = QTextCharFormat() #定义插入的文本格式
tcf.setFontFamily('隶属') #定义格式字体
tcf.setFontPointSize(30) #定义格式字体大小
tc.insertText('插入文本',tcf) #按格式插入字体
tc.insertHtml("<a href='http://www.baidu.com'> 百度</a>") #插入html文本(超链接)
如果想查看具体的格式设置,可以按下Ctrl键点击QTextCharFormat看看里面的构造函数,我们在后面还会再详细讲解。这里插入的超链接由于设置的关系不能用鼠标点击,但是已经有超链接的外观效果了。还有一种是插入文本片段
def fun():
tc = te.textCursor()
tdf = QTextDocumentFragment().fromPlainText('123') #插入普通文本片段
tc.insertFragment(tdf)
tdf2 = QTextDocumentFragment.fromHtml("<a href='http://www.baidu.com'> 百度</a>") #插入html标签文本片段
tc.insertFragment(tdf2)
插入图片
def fun():
tc = te.textCursor()
pic = QTextImageFormat()
pic.setName('picture.png') #图片路径
pic.setHeight(50) #图片高度
pic.setWidth(50) #图片宽度
tc.insertImage(pic) #插入图片
这里的picture.png是在同目录下一个图片。还有几个已经过期但还可用的API,可以了解一下
tc.insertImage(pic,QTextFrameFormat) #按格式插入图片
tc.insertImage('picture.png') #直接按文件名插入图片
插入列表
还可以在QTextEdit里插入一个列表,在输入文字的时候可以有下面的效果
def fun():
tc = te.textCursor()
tlf = QTextListFormat.ListCircle
tc.insertList(QTextListFormat.ListCircle) #插入列表,并把光标右边的字符置为列表第一项
tc.insertList(tlf) #在当前位置插入一个新块,并使其成为具有给定格式的新创建列表的第一个列表项,返回创建的列表
tc.createList(QTextListFormat.ListCircle) #创建列表,并把光标所在行(段落)的字符置为列表第一项
tc.createList(tlf)
#常用列表样式枚举值
QTextListFormat.ListDisc #圆点
QTextListFormat.ListCircle #空心圆
QTextListFormat.ListSquare #方块
QTextListFormat.ListDecimal #数字升序
QTextListFormat.ListUpperRoman #大写罗马数字
QTextListFormat.ListLowerRoman #小写罗马数字
QTextListFormat.ListUpperAlpha #大写拉丁字母
QTextListFormat.ListLowerAlpha #小写拉丁字母
除了上面的枚举值,还有另外一种设定方法
def fun():
tc = te.textCursor()
tlf = QTextListFormat()
tlf.setIndent(3) #设定缩紧
tlf.setNumberPrefix('<<') #如果列表值为数字,设定数字前缀
tlf.setNumberSuffix('>>>') #如果列表值为数字,设定数字后缀
tlf.setStyle(QTextListFormat.ListDecimal) #样式设置
t2 = tc.insertList(tlf)
插入表格
def fun():
tc = te.textCursor()
# tc.insertTable(10,5) #直接插入表格(行,列)
ttf = QTextTableFormat() #创建表格对象格式
tc.insertTable(10,5,ttf) #按格式插入表格
下面是常用的格式定义
ttf = QTextTableFormat() #创建表格对象格式 ttf.setCellPadding(10) #单元格内文本和边框距离 ttf.setCellSpacing(20) #单元格线宽 ttf.setAlignment(Qt.AlignRight) #对齐模式
插入表格时,把插入的表格赋值给一个变量,还可以对这个表格进行一系列QTextTable里的各种操作,这里列举几个效果
def fun():
tc = te.textCursor()
ttf = QTextTableFormat()
ttf.setCellPadding(10)
table = tc.insertTable(3,2,ttf)
table.appendColumns(3) #追加列
table.appendRows(2) #追加行
table.mergeCells(1,2,2,3) #合并单元格(第1行第2列开始,合并2行3列)
下面是QTextTable里的各种功能。
class QTextTable(QTextFrame):
def __init__(self, doc: QTextDocument) -> None: ...
def appendColumns(self, count: int) -> None: ...
def appendRows(self, count: int) -> None: ...
def setFormat(self, aformat: QTextTableFormat) -> None: ...
def format(self) -> QTextTableFormat: ...
def rowEnd(self, c: QTextCursor) -> QTextCursor: ...
def rowStart(self, c: QTextCursor) -> QTextCursor: ...
@typing.overload
def cellAt(self, row: int, col: int) -> QTextTableCell: ...
@typing.overload
def cellAt(self, position: int) -> QTextTableCell: ...
@typing.overload
def cellAt(self, c: QTextCursor) -> QTextTableCell: ...
def columns(self) -> int: ...
def rows(self) -> int: ...
def splitCell(self, row: int, col: int, numRows: int, numCols: int) -> None: ...
@typing.overload
def mergeCells(self, row: int, col: int, numRows: int, numCols: int) -> None: ...
@typing.overload
def mergeCells(self, cursor: QTextCursor) -> None: ...
def removeColumns(self, pos: int, num: int) -> None: ...
def removeRows(self, pos: int, num: int) -> None: ...
def insertColumns(self, pos: int, num: int) -> None: ...
def insertRows(self, pos: int, num: int) -> None: ...
def resize(self, rows: int, cols: int) -> None: ...
def setColumnWidthConstraints(self, constraints: typing.Iterable[QTextLength]) -> None: ...
可以看出来,函数传递参数的是一个可迭代的对象,并且定义了typing(QTextLength的构造函数标注了两种长度值:百分比(PercentageLength)和像素值(FixedLength))所以用起来是这样的
def fun():
tc = te.textCursor()
ttf = QTextTableFormat()
# ttf.setColumnWidthConstraints((QTextLength(QTextLength.PercentageLength,60),QTextLength(QTextLength.PercentageLength,40))) #百分比定义列宽
ttf.setColumnWidthConstraints((QTextLength(QTextLength.FixedLength,80),QTextLength(QTextLength.FixedLength,70))) #像素定义列宽
table = tc.insertTable(3,2,ttf)
本文介绍了QTextEdit在Python中的使用,涵盖了文本插入、格式设置(包括HTML标签、图片、列表和表格)、以及相关功能如文本追加、清除和格式控制。重点展示了如何使用QTextCharFormat、QTextTable等进行文本样式和结构的定制。

8476

被折叠的 条评论
为什么被折叠?



