Python PyQt4 函数来保存和恢复 UI 小部件值? [英] Python PyQt4 functions to save and restore UI widget values?
问题描述
在我尝试编写自己的 Python PyQt4 模块函数之前......我想问一下是否有人有这样的函数可以分享.
Before I attempt to write my own Python PyQt4 module functions... I wanted to ask if anyone has such a function to share.
在我使用 PyQt4 和 qtDesigner 构建 GUI 的许多 Python 程序中,我使用 QSettings 方法在关闭和启动期间保存和恢复所有小部件的 UI 状态和值.
In many of my python programs where I have a GUI built using PyQt4 and qtDesigner, I use the QSettings method to save and restore UI states and values of all widgets during close and startup.
这个例子展示了我如何保存和恢复一些 lineEdit、checkBox 和 radioButton 字段.
This example shows how I save and restore some lineEdit, checkBox, and radioButton fields.
有没有人有一个函数可以遍历 UI 并找到所有小部件/控件及其状态并保存它们(例如 guisave())和另一个可以恢复它们的函数(例如 guirestore())?
Does anyone have a function that can traverse the UI and find ALL widgets/controls and their states and save them (e.g. guisave()) and another function that can restore them (e.g. guirestore())?
我的 closeEvent 看起来像这样:
My closeEvent looks something like this:
#---------------------------------------------
# close by x OR call to self.close
#---------------------------------------------
def closeEvent(self, event): # user clicked the x or pressed alt-F4...
UI_VERSION = 1 # increment this whenever the UI changes significantly
programname = os.path.basename(__file__)
programbase, ext = os.path.splitext(programname) # extract basename and ext from filename
settings = QtCore.QSettings("company", programbase)
settings.setValue("geometry", self.saveGeometry()) # save window geometry
settings.setValue("state", self.saveState(UI_VERSION)) # save settings (UI_VERSION is a constant you should increment when your UI changes significantly to prevent attempts to restore an invalid state.)
# save ui values, so they can be restored next time
settings.setValue("lineEditUser", self.lineEditUser.text());
settings.setValue("lineEditPass", self.lineEditPass.text());
settings.setValue("checkBoxReplace", self.checkBoxReplace.checkState());
settings.setValue("checkBoxFirst", self.checkBoxFirst.checkState());
settings.setValue("radioButton1", self.radioButton1.isChecked());
sys.exit() # prevents second call
我的 MainWindow init 看起来像这样:
My MainWindow init looks something like this:
def __init__(self, parent = None):
# initialization of the superclass
super(QtDesignerMainWindow, self).__init__(parent)
# setup the GUI --> function generated by pyuic4
self.setupUi(self)
#---------------------------------------------
# restore gui position and restore fields
#---------------------------------------------
UI_VERSION = 1
settings = QtCore.QSettings("company", programbase) # http://pyqt.sourceforge.net/Docs/PyQt4/pyqt_qsettings.html
self.restoreGeometry(settings.value("geometry"))
self.restoreState(settings.value("state"),UI_VERSION)
self.lineEditUser.setText(str(settings.value("lineEditUser"))) # restore lineEditFile
self.lineEditPass.setText(str(settings.value("lineEditPass"))) # restore lineEditFile
if settings.value("checkBoxReplace") != None:
self.checkBoxReplace.setCheckState(settings.value("checkBoxReplace")) # restore checkbox
if settings.value("checkBoxFirst") != None:
self.checkBoxFirst.setCheckState(settings.value("checkBoxFirst")) # restore checkbox
value = settings.value("radioButton1").toBool()
self.ui.radioButton1.setChecked(value)
推荐答案
好的,我写了一个包含 2 个函数的模块来完成我的要求.没那么复杂,一旦我想通了,但是当您创建新的 pyqt gui 程序时,它确实可以节省大量时间,您希望在会话之间保存小部件字段值.我目前只有 lineEdit、checkBox 和 combobox 字段编码.如果其他人想要添加或改进(例如单选按钮......等)......我相信其他人,包括我自己,都会很感激.
OK, I wrote a module with 2 functions to do what I was asking for. Not really that complicated, once I figured it out, but it sure does save a lot of time whenever you create new pyqt gui programs where you want to save widget field values between sessions. I currently only have lineEdit, checkBox and combobox fields coded. If anyone else wants to add or improve (e.g. radio buttons...etc) ... I'm sure others, including myself, will appreciate it.
#===================================================================
# Module with functions to save & restore qt widget values
# Written by: Alan Lilly
# Website: http://panofish.net
#===================================================================
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import inspect
#===================================================================
# save "ui" controls and values to registry "setting"
# currently only handles comboboxes editlines & checkboxes
# ui = qmainwindow object
# settings = qsettings object
#===================================================================
def guisave(ui, settings):
#for child in ui.children(): # works like getmembers, but because it traverses the hierarachy, you would have to call guisave recursively to traverse down the tree
for name, obj in inspect.getmembers(ui):
#if type(obj) is QComboBox: # this works similar to isinstance, but missed some field... not sure why?
if isinstance(obj, QComboBox):
name = obj.objectName() # get combobox name
index = obj.currentIndex() # get current index from combobox
text = obj.itemText(index) # get the text for current index
settings.setValue(name, text) # save combobox selection to registry
if isinstance(obj, QLineEdit):
name = obj.objectName()
value = obj.text()
settings.setValue(name, value) # save ui values, so they can be restored next time
if isinstance(obj, QCheckBox):
name = obj.objectName()
state = obj.checkState()
settings.setValue(name, state)
#===================================================================
# restore "ui" controls with values stored in registry "settings"
# currently only handles comboboxes, editlines &checkboxes
# ui = QMainWindow object
# settings = QSettings object
#===================================================================
def guirestore(ui, settings):
for name, obj in inspect.getmembers(ui):
if isinstance(obj, QComboBox):
index = obj.currentIndex() # get current region from combobox
#text = obj.itemText(index) # get the text for new selected index
name = obj.objectName()
value = unicode(settings.value(name))
if value == "":
continue
index = obj.findText(value) # get the corresponding index for specified string in combobox
if index == -1: # add to list if not found
obj.insertItems(0,[value])
index = obj.findText(value)
obj.setCurrentIndex(index)
else:
obj.setCurrentIndex(index) # preselect a combobox value by index
if isinstance(obj, QLineEdit):
name = obj.objectName()
value = unicode(settings.value(name)) # get stored value from registry
obj.setText(value) # restore lineEditFile
if isinstance(obj, QCheckBox):
name = obj.objectName()
value = settings.value(name) # get stored value from registry
if value != None:
obj.setCheckState(value) # restore checkbox
#if isinstance(obj, QRadioButton):
################################################################
if __name__ == "__main__":
# execute when run directly, but not when called as a module.
# therefore this section allows for testing this module!
#print "running directly, not as a module!"
sys.exit()
这篇关于Python PyQt4 函数来保存和恢复 UI 小部件值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!