栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python PyQt4函数可用于保存和恢复UI小部件值吗?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python PyQt4函数可用于保存和恢复UI小部件值吗?

好的,我编写了一个具有2个功能的模块来完成我所要求的。一旦我弄清楚了,它并没有那么复杂,但是当您创建新的pyqt
gui程序要在会话之间保存窗口小部件字段值时,确实可以节省很多时间。我目前只有lineEdit,checkBox和combobox字段编码。如果其他任何人想要添加或改进(例如单选按钮等)…我敢肯定,包括我自己在内的其他人也会喜欢它。

#===================================================================# Module with functions to save & restore qt widget values# Written by: Alan Lilly # Website: http://panofish.net#===================================================================import sysfrom 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 = unipre(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 = unipre(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()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/639364.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号