Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing keyPressEvent in QWidget

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.

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

This is what I have right now:

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):
         ...
     ...

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.

I looked at this question beforehand: PyQt Connect to KeyPressEvent

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

like image 292
Peter Wang Avatar asked Dec 01 '22 12:12

Peter Wang


1 Answers

You can do two ways and one is simply re implement keyPressevent with out any fancy work. Like this

from PyQt4 import QtCore, QtGui
import sys

class Example(QtGui.QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.setGeometry(300, 300, 250, 150)
        self.show()

    def keyPressEvent(self, event):
        if event.key() == QtCore.Qt.Key_Q:
            print "Killing"
            self.deleteLater()
        elif event.key() == QtCore.Qt.Key_Enter:
            self.proceed()
        event.accept()

    def proceed(self):
        print "Call Enter Key"

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

Or as you tried with signals, in your case you where missing to implement this signal properly, here is updated version.

class Example(QtGui.QWidget):
    keyPressed = QtCore.pyqtSignal(QtCore.QEvent)
    def __init__(self):
        super(Example, self).__init__()
        self.setGeometry(300, 300, 250, 150)
        self.show()
        self.keyPressed.connect(self.on_key)

    def keyPressEvent(self, event):
        super(Example, 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:
            print "Killing"
            self.deleteLater()  # a test I implemented to see if pressing 'Q' would close the window

    def proceed(self):
        print "Call Enter Key"

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
like image 90
Achayan Avatar answered Dec 05 '22 19:12

Achayan