alphanumeric
alphanumeric

Reputation: 19349

How to query widgets attached to QLayout

I would like to iterate through every widget attached to HBoxLayout.

    myLayout=QtGui.QHBoxLayout() 

    for i in range(myLayout.count()):
        print i

But I am getting an Attribute Error: 'QHBoxLayout' object has no attribute 'item' on:

        item=self.ComboBoxQHBoxLayout.item(i)

What would be a proper syntax to query the widgets attached to layout using its index number?

Upvotes: 0

Views: 68

Answers (1)

Avaris
Avaris

Reputation: 36725

As the error states, layouts don't have item. However, they do have itemAt that returns QLayoutItem. You can get the widget out of QLayoutItem using widget() method. If the item was not a widget (i.e. another layout or spacer) None will be returned.

for i in range(myLayout.count()):
    widget = myLayout.itemAt(i).widget()
    if widget:
        # item is a widget
        print widget

Upvotes: 3

Related Questions