Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

index error with QSettingsManager #30

Open
mc3 opened this issue Jul 21, 2021 · 1 comment
Open

index error with QSettingsManager #30

mc3 opened this issue Jul 21, 2021 · 1 comment

Comments

@mc3
Copy link

mc3 commented Jul 21, 2021

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants