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)
layout.addWidget(button)
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With