我在python编程世界中还是一个新手。我正在编写一个GUI,允许用户插入一些数据。我正在使用PyQt5,并尝试使用这些功能在重新打开应用程序后从.ini文件中保存和重新加载小部件状态。
from PyQt5 import QtCore, QtGui, QtWidgets
def value_is_valid(val):
if isinstance(val, QtGui.QPixmap):
return not val.isNull()
return True
def restore(settings):
finfo = QtCore.QFileInfo(settings.fileName())
if finfo.exists() and finfo.isFile():
for w in QtWidgets.qApp.allWidgets():
if w.objectName():
mo = w.metaObject()
for i in range(mo.propertyCount()):
prop = mo.property(i)
name = prop.name()
last_value = w.property(name)
key = "{}/{}".format(w.objectName(), name)
if not settings.contains(key):
continue
val = settings.value(key, type=type(last_value),)
if (
val != last_value
and value_is_valid(val)
and prop.isValid()
and prop.isWritable()
):
w.setProperty(name, val)
def save(settings):
for w in QtWidgets.qApp.allWidgets():
if w.objectName():
mo = w.metaObject()
for i in range(mo.propertyCount()):
prop = mo.property(i)
name = prop.name()
key = "{}/{}".format(w.objectName(), name)
val = w.property(name)
if value_is_valid(val) and prop.isValid() and prop.isWritable():
settings.setValue(key, w.property(name))
我想在我的表单中从QListWidgets保存并重新加载项目。目前还没有发生。任何帮助和建议,表示赞赏。谢谢
这是如何使用这些功能的示例。如您所见,可以恢复@NicholasTJ行编辑,但不能恢复您在QListWidget中添加的项目。
from PyQt5.QtCore import *
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(366, 376)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(60, 70, 93, 28))
self.pushButton.setObjectName("pushButton")
self.listWidget = QtWidgets.QListWidget(Form)
self.listWidget.setGeometry(QtCore.QRect(60, 110, 256, 192))
self.listWidget.setObjectName("listWidget")
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(170, 70, 113, 21))
self.lineEdit.setObjectName("lineEdit")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "ADD"))
class MyForm(QtWidgets.QWidget):
settings = QSettings("gui.ini", QSettings.IniFormat)
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
restore(self.settings)
self.ui.pushButton.clicked.connect(self.addbutton)
def addbutton(self):
if self.ui.lineEdit.text():
self.ui.listWidget.addItem(self.ui.lineEdit.text().upper())
self.ui.lineEdit.setText("")
def closeEvent(self, event):
save(self.settings)
QtWidgets.QWidget.closeEvent(self, event)
def value_is_valid(val):
if isinstance(val, QtGui.QPixmap):
return not val.isNull()
return True
def restore(settings):
finfo = QtCore.QFileInfo(settings.fileName())
if finfo.exists() and finfo.isFile():
for w in QtWidgets.qApp.allWidgets():
if w.objectName():
mo = w.metaObject()
for i in range(mo.propertyCount()):
prop = mo.property(i)
name = prop.name()
last_value = w.property(name)
key = "{}/{}".format(w.objectName(), name)
if not settings.contains(key):
continue
val = settings.value(key, type=type(last_value),)
if (
val != last_value
and value_is_valid(val)
and prop.isValid()
and prop.isWritable()
):
w.setProperty(name, val)
def save(settings):
for w in QtWidgets.qApp.allWidgets():
if w.objectName():
mo = w.metaObject()
for i in range(mo.propertyCount()):
prop = mo.property(i)
name = prop.name()
key = "{}/{}".format(w.objectName(), name)
val = w.property(name)
if value_is_valid(val) and prop.isValid() and prop.isWritable():
settings.setValue(key, w.property(name))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
form = MyForm()
form.show()
sys.exit(app.exec_())