0

With the code below, every key prints something in the terminal except for the tab key. The tab key still works though, I'm able to tab between line edits. I just can capture the event.

# i'm using PyQt5==5.11.3 and 32 bit python 3.7.1

from PyQt5.QtWidgets import QLineEdit, QLabel, QWidget, QVBoxLayout, QApplication
import sys

class Main(QWidget):
    def __init__(self):
        super().__init__()
        label = QLabel('event')
        input1 = Input()
        input2 = Input()
        layout = QVBoxLayout()
        layout.addWidget(label)
        layout.addWidget(input1)
        layout.addWidget(input2)
        self.setLayout(layout)
        self.show()

class Input(QLineEdit):
    def __init__(self):
        super().__init__()

    def keyPressEvent(self, event):
        # why doesn't tab print anything
        print(event.key())


if __name__ == "__main__":
    app = QApplication(sys.argv)
    wid = Main()
    sys.exit(app.exec_())
mchant
  • 320
  • 1
  • 8
  • Have a look at [this thread](https://stackoverflow.com/questions/53856338/how-to-allow-qlineedit-detects-tab-key-pressing-event) – olinox14 Jan 21 '19 at 13:27
  • That captures key press, but now it doesn't process the event normally (it doesn't tab between the line edits). – mchant Jan 21 '19 at 13:59
  • try to add `event.ignore()` at the end of your method? – olinox14 Jan 21 '19 at 14:22
  • I don't know where to add that, but I think I can get it to work if I use pyQtSignal to inform the parent, then use the parent to move to the next input. – mchant Jan 21 '19 at 14:46

1 Answers1

1

You can intercept tke Tab pressed event using the event method from QLineEdit. You process your event, then you pass it to the QLineEdit.event() method.

Something like that:

import sys

from PyQt5.QtCore import QEvent, Qt
from PyQt5.QtWidgets import QLineEdit, QLabel, QWidget, QVBoxLayout, QApplication

class Main(QWidget):
    def __init__(self):
        super().__init__()
        label = QLabel('event')
        input1 = Input()
        input2 = Input()
        layout = QVBoxLayout()
        layout.addWidget(label)
        layout.addWidget(input1)
        layout.addWidget(input2)
        self.setLayout(layout)
        self.show()

class Input(QLineEdit):
    def __init__(self):
        super().__init__()

    def keyPressEvent(self, event):
        print(event.key())

    def event(self,event):
        if event.type() == QEvent.KeyPress and event.key() == Qt.Key_Tab:
            self.tabFollow()
        return QLineEdit.event(self,event)

    def tabFollow(self):
        print("tab-key pressed!")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    wid = Main()
    sys.exit(app.exec_())
olinox14
  • 6,177
  • 2
  • 22
  • 39
  • Thanks. This works. I tried this in the event method just with `print(event.type())` to see what prints, and it kept crashing with `TypeError: invalid result from Input.event(), a 'bool' is expected not 'NoneType'`. I didn't know it needed `return QLineEdit.event(self,event)`. – mchant Jan 21 '19 at 19:52