My question is somewhat related to Getting layout widgets in PyQT , but this is not a duplicate. Instead of looking for a high-level strategic view of how to do this, I try to understand what might be the most idiomatic and easiest way to do this. Since PyQt is a fairly accurate Qt C ++ API binding, it provides a C-ish way to get widgets in a layout. Here is the idiom I used:
for i in range(layout.count()): item = layout.itemAt(i) if type(item) == QtGui.QLayoutItem: doSomeStuff(item.layout()) if type(item) == QtGui.QWidgetItem: doSomething(item.widget())
I'm not the most experienced guy in Python, but that seems messy. My intuition tells me that in an ideal world, Python code should look something like this:
for w in layout.widgets(): doSomething(w)
Am I really wrong? Did I miss the highest idiom? Is this the best possible way to iterate over widgets in PyQt? I tend to think in C ++, so sometimes I miss the "obvious" features of the Python language that improve the situation. Part of what I am doing is recursively descending into widgets with layouts with widgets with layouts (etc.), to automatically connect to the user interface created in Designer at runtime. Add QTabWidgets and handle the dynamic properties set in the designer, and my code basically works, but it just feels awfully awkward.
source share