I’m trying to refresh the list widget after pressing the update button.
I’m making a function called refresh() but I think I need some help.
Thanks
As Barry says, it would help to see your complete code. Ideally, as
small as possible but complete enough to run or at least see everything
which is going on.
My function:
Please paste code inside “code fences” (triple backticks). This
preserves indentation and other punctuation, which is import for code.
Eg:
```
your code or programme output goes here
```
There’s a </>
button on the message composition toolbar to do this for
you.
Your code:
def refresh(self):
self.i_listwidget.clear()
for record in records:
self.i_listwidget.addItem(str(record[0]))
This looks like an object method (it has a self
parameter) rather than
a plain function. Is this from inside a class definition.
I’m going to assume that here records
is a list of database query
result rows, which typically come back from an SQL query. If so, the
code looks ok and the most obvious problem is that records
is not
defined.
Usually you’d just pass it as a parameter, eg:
def refresh(self, records):
self.i_listwidget.clear()
for record in records:
self.i_listwidget.addItem(str(record[0]))
If you’ve got a class inside with this referesh method, let’s call it
foo
, then you’d use it like:
... code getting the db query and refreshing the widget ...
query_records = ... db query call goes here ...
foo.refresh(query_records)
Notice that the variable at the calling end can be whatever useful name
you like (query_records
in the example above) and the refresh
function still calls it records
internally because that is the name
of the function parameter.
Cheers,
Cameron Simpson cs@cskk.id.au