Python PyQt4 functions to save and restore UI widget values?

前端 未结 7 817
悲哀的现实
悲哀的现实 2020-12-04 13:50

Before I attempt to write my own Python PyQt4 module functions... I wanted to ask if anyone has such a function to share.

In many of my python programs where I hav

相关标签:
7条回答
  • 2020-12-04 14:40

    Here's an updated snippet which originally shared by mr. Panofish. Those great functions are same, but now can be used on never versions of PyQt and Python with minor changes if needed. Thx mr. Panofish, long live OpenSource! :)

    Changes:

    • Updated for Python3 and PyQt5
    • Added geometry save\restore
    • Added QRadioButton save\restore
    • SetCheckState() replcaed with SetChecked() in order to avoid tristate

      def guisave(self):
      
        # Save geometry
          self.settings.setValue('size', self.size())
          self.settings.setValue('pos', self.pos())
      
          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.isChecked()
                settings.setValue(name, state)
      
            if isinstance(obj, QRadioButton):
                name = obj.objectName()
                value = obj.isChecked()  # get stored value from registry
                settings.setValue(name, value)
      
      
      def guirestore(self):
      
        # Restore geometry  
        self.resize(self.settings.value('size', QtCore.QSize(500, 500)))
        self.move(self.settings.value('pos', QtCore.QPoint(60, 60)))
      
        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 = (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 = (settings.value(name).decode('utf-8'))  # 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.setChecked(strtobool(value))  # restore checkbox
      
            if isinstance(obj, QRadioButton):
               name = obj.objectName()
               value = settings.value(name)  # get stored value from registry
               if value != None:
                   obj.setChecked(strtobool(value))
      
    0 讨论(0)
提交回复
热议问题