PDA

View Full Version : Unable to send signal from QPushButton created in Python loop



nlgootee
28th October 2016, 18:58
I am trying to create a series of buttons in a loop. Each button gets an id number as it's text and when the button is clicked it is supposed to send the id number to a function that will open an archived order. At this time I just want to print the order number to prove that the signal works and each button is connected to the correct order number.

ui.cmdOpen = QtWidgets.QPushButton(ui.frOrdHist)
ui.cmdOpen.setGeometry(QtCore.QRect(269, line1Y, 61, 22))
ui.cmdOpen.setText(iOrderId)
ui.cmdOpen.setObjectName("cmdOpen")
ui.cmdOpen.clicked.connect(lambda button=ui.cmdOpen:displayOrder(ui, button))


def displayOrder(ui, button):
i = button.text()
print(i)

When I click the button, I get an error message that says "boolean object has no text attribute"

I tried passing the order number directly and it would print "False" so still a boolean. I don't know where the boolean is coming from, it must be something wrong in the signal.

anda_skoa
29th October 2016, 09:19
No idea about Python lambdas but you could use a QButtonGroup and simply connect to the group's buttonClicked() (http://doc.qt.io/qt-5/qbuttongroup.html#buttonClicked) signal.

Cheers,
_

Killian
31st October 2016, 11:40
It's most likely your lambda definition that is causing this confusion. The signal is correct and that boolean is coming from the signal's standard parameter. Have a look at : http://doc.qt.io/qt-5/qabstractbutton.html#clicked
As far as I remember your button parameter will be overwritten with that boolean, so you should rather do something like this:

ui.cmdOpen.clicked.connect(lambda state, button=ui.cmdOpen:displayOrder(button))

nlgootee
3rd November 2016, 16:41
That is pretty much the correct answer, the code that works is:

ui.cmdOpen.clicked.connect(lambda checked, button=ui.cmdOpen:displayOrder(button))

The documentation states that the checkable state is false by default and if it is not checkable this should not be an issue, so I don't understand where this comes from. I use lots of pushbuttons without this problem but this is the only one that I have that passes an argument.