I am trying to set the selected lines to bullet points and back, here i set the indent to 0, it destroys the bullet points, yet the list property remains true, so this code will not set the same line back to bullet list again, how to clear the underlying list format, Or what is the best approach?
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()
Items must be removed from the list, and you should also not use createList
again.
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
else:
listFormat = QTextListFormat()
style = QTextListFormat.ListDisc
listFormat.setStyle(style)
cursor.createList(listFormat)
self.textEdit.setTextCursor(cursor)
self.textEdit.setFocus()
Note: list
is a Python builtin, assigning it to anything else is considered bad practice.