Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-1216

Have QListWidget behave as an iterable

    XMLWordPrintable

    Details

    • Type: Suggestion
    • Status: Reported
    • Priority: Not Evaluated
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: PySide
    • Labels:
      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

        Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

            Assignee:
            crmaurei Cristian Maureira-Fredes
            Reporter:
            xrogaan Ludovic Bellière
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:

                Gerrit Reviews

                There are no open Gerrit changes