Python PyQt4 functions to save and restore UI widget values?

前端 未结 7 831
悲哀的现实
悲哀的现实 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:25

    I'm a novice programmer so I'm not sure where I was going wrong and please excuse my lack of proper terminology/ technical understanding but my application has multiple QWidget classes for different app 'states' that load/ unload different UI's(and related functions) per state - all in and out of a single main 'QMainWindow'

    this is using @beesleep class implementation but it looks like the code in question is the same for all the above examples

    I was having issues with -

    def _is_handled_type(cls, widget):
        return any(isinstance(widget, t) for t in cls._get_handled_types())
    
    
    for name, obj in inspect.getmembers(self)
        # print(name) <------------------------------
        if not self._is_handled_type...
    

    The methods of self "QMainWindow" are returned instead of all active child widgets.

    .

    My fix was this. I deleted - ***this part is not necessary, the code is just no longer used

    @classmethod
    def _is_handled_type(cls, widget):
        return any(isinstance(widget, t) fort in cls._get_handled_types())
    

    Then changed in _gui_save() and _gui_restore() -

    for name, obj in inspect.getmembers(self):
        if not self._is_handled_type(obj):
            continue
        if name in self._names_to_avoid: # _gui_restore()
            continue                     # _gui_restore()
            name = obj.objectName()
            value = None
    

    To -

    for child in self._get_handled_types():
    
        for obj in self.findChildren(child):
    
            if obj:
    
                name = obj.objectName()
                if name in self._names_to_avoid: # _gui_restore()
                    continue                     # _gui_restore()
                value = None
    

    The last step is that you have to assign a name to each object that you want to save

    self.q_list = QListWidget()
    self.q_list.setObjectName("List")
    ...
    self.q_text = QLineEdit('enter text')
    self.q_text.setObjectName("Text")
    

    .

    for child in self._get_handled_types():
        for obj in self.findChildren(child):
            if obj:
                name = obj.objectName()
    
                print(name, obj) <-------------
    

    Returns

    Text 
    List 
    

    final note... if you want to see where the settings file is being saved then add - print(self.settings.fileName()) under the init

    cheers!

提交回复
热议问题