Python GUI编程网 > PyQt5控件_PyQt5控件方法大全 > 多行文本框QTextEdit文本光标完成创建列表createList()、插入列表insertList()和列表样式设置QTextListFormat()
python教程

多行文本框QTextEdit文本光标完成创建列表createList()、插入列表insertList()和列表样式设置QTextListFormat()

发布博客:2022年12月6日 17:52
阅读:351
作者:Python GUI编程网

QTextEdit文本光标创建和插入列表主要使用到2个方法createList()和insertList(),并且可以通过QTextListFormat()来设置列表样式。这些操作也是借助于光标来进行操作的,下面就来详细讲解一下。

QTextEdit文本光标创建和插入列表主要使用到2个方法createList()和insertList(),并且可以通过QTextListFormat()来设置列表样式。这些操作也是借助于光标来进行操作的,下面就来详细讲解一下。

创建列表createList()、插入列表insertList()都有2中不同的方案,这两者之间的区别就是createList()会把光标所在行或者段落作为列表的开始项,而insertList()会把光标所在位置的右侧作为列表的开始项。

第一种是直接通过给定的QTextListFormat()对象的方法来设置列表样式,这种设置方法也要结合下面的枚举类型来设置详细的样式和前缀。

# 列表样式设置方法
# QTextListFormat.setStyle()  # 设置列表样式
# QTextListFormat.setNumberPrefix()  # 设置数字前缀
# QTextListFormat.setNumberSuffix()  # 设置数字后缀
# QTextListFormat.setIndent()  # 设置列表缩进,数字代表缩进的Tab次数

下面是完整代码

from PyQt5.Qt import *
import sys
 
 
 class Window(QWidget):
     def __init__(self):
         super().__init__()
         self.setWindowTitle("QTextEdit - PyQt5中文网")
         self.resize(600, 500)
         self.func_list()
 
     def func_list(self):
         self.func()
 
     def func(self):
         self.qte = QTextEdit('QTextEdit构造', self)
         self.qte.move(100, 100)
         self.qte.resize(250, 250)
 
         self.btn = QPushButton('按  钮', self)
         self.btn.move(120, 50)
         self.btn.resize(70, 30)
         self.btn.pressed.connect(self.text_cur)
 
     # 文本光标完成内容和格式设置
     def text_cur(self):
         # 1.首先创建一个光标对象
         qtc = self.qte.textCursor()
         # 3.插入文本内容格式
         tcf = QTextListFormat()
         tcf.setNumberPrefix('$')  # 列表前缀,必须要加上setStyle,设置为数字
         tcf.setIndent(2)  # 缩进一个Tab
         tcf.setStyle(QTextListFormat.ListDecimal)
         # 2.通过光标插入文本,传入上面的格式对象
         qtc.insertList(tcf)
 
 
 if __name__ == '__main__':
     app = QApplication(sys.argv)
     window = Window()
 
     window.show()
     sys.exit(app.exec_())

第二种是通过QTextListFormat()类中的枚举值给定列表样式。

# 列表前缀图标枚举样式
# QTextListFormat.ListCircle  # 一个空心圆圈
# QTextListFormat.ListDecimal  # 十进制数字
# QTextListFormat.ListDisc  # 一个实心圆圈
# QTextListFormat.ListSquare  # 一个方块
# QTextListFormat.ListLowerAlpha  # 小写拉丁字母
# QTextListFormat.ListUpperAlpha  # 大写拉丁字母
# QTextListFormat.ListLowerRoman  # 小写罗马字母
# QTextListFormat.ListUpperRoman  # 大写罗马字母

下面是完整代码,两种创建方案对于列表起始位置的不同请格外注意。

from PyQt5.Qt import *
import sys
 
 
 class Window(QWidget):
     def __init__(self):
         super().__init__()
         self.setWindowTitle("QTextEdit - PyQt5中文网")
         self.resize(600, 500)
         self.func_list()
 
     def func_list(self):
         self.func()
 
     def func(self):
         self.qte = QTextEdit('QTextEdit构造', self)
         self.qte.move(100, 100)
         self.qte.resize(250, 250)
 
         self.btn = QPushButton('按  钮', self)
         self.btn.move(120, 50)
         self.btn.resize(70, 30)
         self.btn.pressed.connect(self.text_cur)
 
     # 文本光标完成内容和格式设置
     def text_cur(self):
         # 插入列表1
         # 1.首先创建一个光标对象
         qtc = self.qte.textCursor()
         # 2.通过光标插入列表,传入上面的格式对象
         qtc.insertList(QTextListFormat.ListDecimal)  # 返回一个QTextList对象
         # qtc.createList(QTextListFormat.ListDecimal)  # 注意光标所在位置的变化对创建列表的影响
 
 
 if __name__ == '__main__':
     app = QApplication(sys.argv)
     window = Window()
 
     window.show()
     sys.exit(app.exec_())