python - 如何将项目符号列表重置为普通文本并返回 QTextEdit
问题描述
我正在尝试将选定的行设置为项目符号并返回,这里我将缩进设置为 0,它会破坏项目符号,但列表属性仍然为 true,因此此代码不会再次将同一行设置回项目符号列表,如何清除底层列表格式,或者最好的方法是什么?
def bullet_list(self):
cursor = self.textEdit.textCursor()
list = cursor.currentList()
if list:
listfmt = cursor.currentList().format()
listfmt.setIndent(0)
cursor.createList(listfmt)
self.textEdit.setTextCursor(cursor)
self.textEdit.setFocus()
else:
listFormat = QTextListFormat()
style = QTextListFormat.Style.ListDisc
listFormat.setStyle(style)
cursor.createList(listFormat)
self.textEdit.setTextCursor(cursor)
self.textEdit.setFocus()
解决方案
项目必须从列表中删除,您也不应该createList
再次使用。
def bullet_list(self):
cursor = self.textEdit.textCursor()
textList = cursor.currentList()
if textList:
start = cursor.selectionStart()
end = cursor.selectionEnd()
removed = 0
for i in range(textList.count()):
item = textList.item(i - removed)
if (item.position() <= end and
item.position() + item.length() > start):
textList.remove(item)
blockCursor = QTextCursor(item)
blockFormat = blockCursor.blockFormat()
blockFormat.setIndent(0)
blockCursor.mergeBlockFormat(blockFormat)
removed += 1
self.textEdit.setTextCursor(cursor)
self.textEdit.setFocus()
else:
listFormat = QTextListFormat()
style = QTextListFormat.ListDisc
listFormat.setStyle(style)
cursor.createList(listFormat)
self.textEdit.setTextCursor(cursor)
self.textEdit.setFocus()
注意:list
是 Python 内置的,将其分配给其他任何东西都被认为是不好的做法。
推荐阅读
- python - 我想不断迭代这个循环,直到满足长度和 x 值
- python - 如何用 pathlib.Path 表示标准输入和标准错误?
- ansible - 为某些键迭代字典 Ansible
- android-jetpack-compose - 使用bottomsheetscaffold时如何使lazycolumn滚动到最后?
- javascript - 如何在javascript代码中执行onclick方法
- amazon-web-services - AWS kinesis firehouse 无法连接到 aws 私有 api 网关
- firebase-realtime-database - 保存规则时出错 - 第 5 行:应为“{”
- scala - http4s 上 scala 应用程序的访问日志记录
- python - 生成随机数的函数用种子调用其他生成随机数的函数,种子似乎又被使用了
- flutter - 将宽度设置为行的特定子项