Details
-
Suggestion
-
Resolution: Won't Do
-
Not Evaluated
-
None
-
None
-
None
Description
Sometimes we want to walk through the elements of a list in order to do some kind of operation. As a python developer I expect to have a list that behave like a list or, at the very least, provides an interface to easily iterate over the list's items.
The current way to do so is:
for idx in range(0, ListWidget.count()): item = ListWidget.item(idx) do_something_with(item)
A better and less cumbersome way to have access to the list item's would be:
for idx, item in ListWidget: do_something_with(item)
Having a QListWidget behave like an iterator, or have a method return an iterator (ListWidget.iterate()), will allow developers to make full use of python's generator expressions and list comprehensions. It could be used to easily filter or sort a list.
Example:
# Assuming the items present in the QListWidget each have a filter method NewList = [item for item in ListWidget if item.filter(data)]
And if a QListWidget would support being fed from a list of QListWidgetItem (NewList), the possibilities would be endless!
While this suggestion limits itself to QListWidget, other Qt elements would surely benefits from being iterable. For instance, it would probably be very interesting to be able to iterate over a table's columns or rows. Or simply iterate over the children that a layout contains. It may not be wise to iterate over everything, so asking the QWidget to return an iterator based on specified parameters could be a better idea.
To iter over all children whose name ends with "_input" and is not enabled:
for item in QWidget.iter(name="*_input", enabled=False): item.setEnabled(True)
I may be missing something obvious, or an alternative and equally easy way to do what I proposed. Feel free to educate me