[PyQt] default pushbutton not working as expected

Pierre-Louis Bonicoli pierre-louis.bonicoli at gmx.fr
Tue Nov 8 07:49:32 GMT 2011


On 07/11/2011 00:09, Zev Goldstein wrote:
> I created a very basic example dialog with 2 qpushbuttons.  I set default
> to true on one of them and I set autodefault to false on both of them.  I
> then used pyuic4 to convert the .ui to a .py file and made a very simple
> wrapper to connect simple functions to both button's click events.
> 
> The expected behavior is that pressing enter should fire the default
> button's clicked handler no matter which button is currently focused, but
> I'm not getting that behavior.  The default button is not getting clicked
> if i have the other button selected and I press enter.
> 
> I tried building the same example in qt/c++ and I didn't have this issue.
> Can anyone tell me if I'm doing something wrong.  I've attached all 3 files
> I mentioned above.
> 

From
http://developer.qt.nokia.com/doc/qt-4.7/qpushbutton.html
"Default and autodefault buttons decide what happens when the user presses
enter in a dialog. [...] The default button behavior is provided only in
dialogs."

In your example the parent is a QMainWindow not a QDialog, the behaviour
is correct using a QDialog.


Moreover:
> class MyForm(QtGui.QMainWindow):
> 	curDir = None
>     
> 	def __init__(self, parent=None):
> 		QtGui.QWidget.__init__(self, parent)

should be

class MyForm(QtGui.QMainWindow):
	curDir = None

	def __init__(self, parent=None):
		QtGui.QMainWindow.__init__(self, parent)

or you could use 'super'.


You should use the "new style signal and slot":
self.defaultPushButton.clicked.connect(self.defaultClicked)
http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/new_style_signals_slots.html


First line is sufficient, the last two are useless:
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
from PyQt4.QtCore import *


Regards,

Pierre-Louis


More information about the PyQt mailing list