TypeError: cannot pickle 'QListWidgetItem' object
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
uic.loadUi('gui.ui', self)
def _get_saving_types(self) -> list:
return [
(QLineEdit, ["setText", "text"]),
(QCheckBox, ["setChecked", "isChecked"]),
(QListWidget, ["addItems"]),
]
def loadState(self):
settings = QSettings('gui.ini', QSettings.Format.IniFormat)
for type, method in self._get_saving_types():
for widget in self.findChildren(type):
if settings.value(widget.objectName()) is not None:
methodcaller(method[0], settings.value(widget.objectName()))(widget)
pass
def saveState(self):
settings = QSettings('gui.ini', QSettings.Format.IniFormat)
for type, method in self._get_saving_types():
for widget in self.findChildren(type):
if isinstance(widget, QListWidget):
items = []
for index in range(widget.count()):
items.append(widget.item(index))
settings.setValue(widget.objectName(), items) # Тут возникает ошибка
pass
else:
settings.setValue(widget.objectName(), methodcaller(method[1])(widget))
def saveState(self):
settings = QSettings('gui.ini', QSettings.Format.IniFormat)
for type, method in self._get_saving_types():
for widget in self.findChildren(type):
if isinstance(widget, QListWidget):
items = []
for index in range(widget.count()):
items.append(widget.item(index).text()) # Сохраняем текст элемента
settings.setValue(widget.objectName(), items)
else:
settings.setValue(widget.objectName(), methodcaller(method[1])(widget))
def loadState(self):
settings = QSettings('gui.ini', QSettings.Format.IniFormat)
for type, method in self._get_saving_types():
for widget in self.findChildren(type):
if settings.contains(widget.objectName()):
if isinstance(widget, QListWidget):
items = settings.value(widget.objectName(), [])
widget.clear()
widget.addItems(items)
else:
methodcaller(method[0], settings.value(widget.objectName()))(widget)