AlekseyN AlekseyN - 2 months ago 25
Python Question

Layout in PyQt consists of QWidgetItems instead of QPushButtons and has no atribute text

I create a layout and put there 4 buttons via for loop.

layout = QtGui.QVBoxLayout()
my_list = ['1', '2', '3', '4']
for elem in my_list:
button = QtGui.QPushButton(elem)

Then I want to check if any button has a text that I determine in a variable VAR. If it so - do something.

items = (layout.itemAt(i) for i in range(layout.count())) #get list of items in lay

for w in items:
print w
#it gives <PyQt4.QtGui.QWidgetItem object at 0x031F65D0> * 4
#instead of QPushButton.

VAR = '1'
if w.text() == VAR : #Problem here.
print 'I got what I want.'
#the problem is that QWidgetItem has no atribute text.

Please, tell me how to get list of QPushButtons instead of QWidgetItems
or any other solution for this problem.


The documentation for QWidgetItem is quite clear. You just need to do:

for item in items:
    w = item.widget()