如何将项目符号列表重置为普通文本并返回 QTextEdit

How to reset a bulleted list to normal text and back in QTextEdit

我正在尝试将选定的行设置为项目符号点并返回,这里我将缩进设置为 0,它破坏了项目符号点,但列表 属性 仍然为真,因此此代码不会设置同一行再次回到项目符号列表,如何清除底层列表格式,或者最好的方法是什么?

    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 内置函数,将它分配给其他任何东西都被认为是不好的做法。