pythonguis / pyqtconfig

A PyQt config manager. Keep Qt widgets in sync with an config dictionary and/or QSettings object.
Other
133 stars 33 forks source link

index error with QSettingsManager #30

Open mc3 opened 3 years ago

mc3 commented 3 years ago

I’m getting — File "/Users/ajr/Projects/NET/zad/zad/views/settings.py", line 178, in setup sc.add_handler("gen/ignored_nets", sd.ignoredListWidget) File "/usr/local/py_env/zad/lib/python3.9/site-packages/pyqtconfig/config.py", line 877, in add_handler handler.setter(self._get(key)) File "/usr/local/py_env/zad/lib/python3.9/site-packages/pyqtconfig/config.py", line 428, in _set_QListWidget self.findItems( IndexError: list index out of range — This happens if the list corresponding to the QListWidget is not empty. I’m using pyqtconfig.QSettingsManager() and do not touch the QListWidget. Instead I modify the settings by replacing the complete list with a modified (per GUI) one.

This is the code to access and set the list settings:


def getPrefs(self): self.prefs = sc.get(self.listPrefName) return

def addPref(self, value): self.getPrefs() self.prefs.append(value) sc.set(self.listPrefName, self.prefs) self.printSettings('End addPref')

def setPref(self, index, value): self.prefs[index] = value sc.set(self.listPrefName, self.prefs)

def delPref(self, value): self.getPrefs() self.prefs.remove(value) sc.set(self.listPrefName, self.prefs)


self.prefs is the list of values. self.listPrefName is something like ’gen/ip4_nets' sc is the pyqtconfig.QSettingsManager instance.

PedanticHacker commented 3 years ago

This code

if v:
    for s in v:
        self.findItems(
            unicode(self._set_map(s)),
            Qt.MatchExactly)[0].setSelected(True)

should probably be rewritten to

try:
    for s in v:
        self.findItems(
            unicode(self._set_map(s)),
            Qt.MatchExactly
        )[0].setSelected(True)
except (KeyError, TypeError):
    return None

We must catch a KeyError to solve the problem that @mc3 encountered, and also a TypeError to anticipate that v might be None. The code is in pyqtconfig/config.py starting on line 433.