Implementing keyPressEvent in QWidget
<p>I have a QDialog window that has a continue button. The continue button is the default button because whenever I press the enter key, the continue button is pressed. I discovered something strange: when I press the enter key three times, the continue button presses three times. However, when I press it a fourth time, the whole window closes. I have a cancel button right below the continue button that closes the window, but I don't make the cancel button the default button or anything. </p>

<p>I wanted to override the <code>keyPressEvent</code> so that whenever I'm in the window, the enter button will always be connected to the continue button. </p>

<p>This is what I have right now:</p>

<pre><code>class ManualBalanceUI(QtGui.QWidget):
    keyPressed = QtCore.pyqtSignal()

    def __init__(self, cls):
        super(QtGui.QWidget, self).__init__()
        self.window = QtGui.QDialog(None, QtCore.Qt.WindowSystemMenuHint)
        self.ui = uic.loadUi('ManualBalanceUI.ui', self.window)
        self.keyPressed.connect(self.on_key)

    def keyPressEvent(self, event):
        super(ManualBalanceUI, self).keyPressEvent(event)
        self.keyPressed.emit(event) 

    def on_key(self, event):
        if event.key() == QtCore.Qt.Key_Enter and self.ui.continueButton.isEnabled():
            self.proceed()  # this is called whenever the continue button is pressed
        elif event.key() == QtCore.Qt.Key_Q:
            self.window.close()  # a test I implemented to see if pressing 'Q' would close the window
     def proceed(self):
         ...
     ...
</code></pre>

<p>However, this doesn't seem to be doing anything right now. Pressing 'Q' doesn't close the window, and I can't really tell if the 'enter' key is working or not. </p>

<p>I looked at this question beforehand: <a href="https://stackoverflow.com/questions/27475940/pyqt-connect-to-keypressevent">PyQt Connect to KeyPressEvent</a></p>

<p>I also reviewed all the documentation on SourceForge. Any help would be greatly appreciated!</p>