Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
[Solved] Questions about the signal handlers' style (old working but not the new way)? [Python PyQt4]
I am new to PyQt, but I like it since it is very handy! Glad to be here.
I am running into a weird issue that the old way of signal handling worked, but the new way failed. Please let me know if I did anything wrong. It is using PyQt4.
So my application has a main window, and I need to make it pop a QDialog for some warning messages and few buttons that user can interact with (sorta like email I guess). So I create a custom QDialog class, add a QDialogButtonBox with three QPushButton. The idea is when a user click a button, the QDialog will close, and return different int value of exec_() to the main window, so my application can process accordingly.
What I did (and worked) was:
self.aButton = QtGui.QPushButton("Afunc")
self.buttonBox = QtGui.QDialogButtonbox()
self.aButton.addButton (self.aButton, QtGui.QDialogButtonBox.RejectRole)
self.connect(self.aButton, QtCore.SIGNAL("clicked()"), self.Afunc)
However, I was told there is a new style to connect buttons, which is below
So I changed it, but then I can never get the desired return value 3 when press the A button. Also I noticed if changed the button's role from RejectRole to AcceptRole or ActionRole, it will be worse -- the QDialog will not close, and thus my application did not receive any return value from exec__(). Nothing happened.
My questions are:
- Why the new style did not work? Am I missing something?
- Why it has to be RejectRole for the button? I do want some button doing nothing but close the QDialog, pretty much like the OK button, and its default return value is desired. I thought it should be AcceptRole, no?
Thank you very much!
[edit: changed for coding tags @ SGaist]
Hi and welcome to devnet,
The new connection style is only available with Qt 5 so you would also have to use PyQt 5.
Closing the dialog doing nothing correspond to a "rejection". You should connect the QDialogButtonBox (e.g. accepted, rejected) signals to the corresponding QDialog slot. You can use the clicked signal which has a button parameter to set the result accordingly.
Hope it helps
Thank you very much for the help. I did not know the new style is only for Qt 5.
I am not really getting what you suggested for the signal, so could you please list some codes. Thanks!
I ended up used QHBoxLayout instead of the QDialogButtonBox, and everything worked. Thanks for the help!
So yeah, the new style works for me in PyQt4, just a note.
Will close the thread if I can.
What do you mean by QHBoxLayout instead of the QDialogButtonBox ?