首页 > 解决方案 > 如何在 PySide2 中获取 QTextList 的格式

问题描述

我想稍后在我的应用程序中访问一个 QTextList 格式,但它似乎在创建后总是被删除

我已经知道这个format()功能

我以这种方式创建 QTextList:

cursor = self.textEdit.textCursor()
cursor.insertList(QTextListFormat.ListDisc)

稍后在应用程序中,我想取回格式,即 ListDisc 或任何其他格式;我这样做:

list = self.textEdit.textCursor().currentList()
if(list):
    print(list.format())

我在调用 print 的行收到此错误:

RuntimeError: Internal C++ object (PySide2.QtGui.QTextList) already deleted.

编辑:下面的 MCVE 在 PySide2 5.12.1 windows 7 上运行时给了我错误

import sys
from PySide2.QtWidgets import QApplication, QTextEdit, QMainWindow
from PySide2.QtGui import QTextListFormat

class Test(QMainWindow):
    def __init__(self, fileName=None):
        super(Test, self).__init__()

        self.testList()

    def testList(self):

        self.textEdit = QTextEdit(self)
        cursor = self.textEdit.textCursor()
        cursor.createList(QTextListFormat.ListDisc)

        list = self.textEdit.textCursor().currentList()
        if(list):
            print(list.format())

if __name__ == '__main__':
    app = QApplication(sys.argv)

    test = Test()
    test.show()

标签: pythonpyside2

解决方案


推荐阅读