0

I'm trying to get key input and filter in pyqt5 ui, but it's not detecting keypress, only getting 74 and 75 event.

class Ui(QMainWindow):
    def eventFilter(self, obj, event):
        # only returned 74 = 6 ? and 75 = 6 ?
        print("{} = {} ?".format(event.type(), QtCore.QEvent.KeyPress))
        if event.type() == QtCore.QEvent.KeyPress:
            print("key pressed!")
        return super(Ui, self).eventFilter(obj, event)

# main
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Ui(0.1)
    window.installEventFilter(window) # install filter
    app.exec_()
gmma
  • 19
  • 1
  • 4
  • Is this an *actual* [mre]? Because that `0.1` argument of `Ui` is a bit suspicious. – musicamante Nov 16 '22 at 12:36
  • 0.1 in constructor is for parameter in __init__. – gmma Nov 16 '22 at 12:40
  • And, as said, that's not a [mre], since QMainWindow doesn't accept a float in its constructor, which means that you didn't really test the code above. You probably are leaving out parts of your code that are important for the reproducibility of the issue (for instance, you're adding a central widget). We cannot help you if you don't provide us the code that actually reproduces the issue. – musicamante Nov 16 '22 at 12:51
  • There's no point having a widget install an event-filter on itself. And monitoring events on the main window won't catch the key-press events of its child widgets. You either need to install the event-filter on the specific widget you're interested in, or, if you want *all* events, install it on the application object. – ekhumoro Nov 16 '22 at 13:31

0 Answers0