Saltycrane logo

SaltyCrane Blog

Notes on Python, Django, and web development on Ubuntu Linux

    

How to capture the Tab key press event with PyQt 4.3

Normally, pressing the TAB key changes focus among widgets. However, I would like to use the TAB key for other purposes (e.g. tab completion). To gain control of the TAB key press event, I need to subclass my widget and reimplement the QObject.event() event handler. I don't need to re-write the entire event handler. I only need to process TAB key press events. I will pass all other events to the default event handler. The example below subclasses the QLineEdit widget and reimplements the event() method. Pressing the TAB key inside this new widget prints out the text "tab pressed" inside a second QLineEdit box.

The Events and Event Filters Trolltech QT documentation has a good explanation of how this works. My example shows how to use Python and PyQt instead of C++.

import sys
from PyQt4.QtCore import * 
from PyQt4.QtGui import * 

#################################################################### 
def main(): 
    app = QApplication(sys.argv) 
    w = MyWindow() 
    w.show() 
    sys.exit(app.exec_()) 

####################################################################
class MyWindow(QWidget): 
    def __init__(self, *args): 
        QWidget.__init__(self, *args)

        # create objects
        self.la = QLabel("Press tab in this box:")
        self.le = MyLineEdit()
        self.la2 = QLabel("\nLook here:")
        self.le2 = QLineEdit()

        # layout
        layout = QVBoxLayout()
        layout.addWidget(self.la)
        layout.addWidget(self.le)
        layout.addWidget(self.la2)
        layout.addWidget(self.le2)
        self.setLayout(layout)

        # connections
        self.connect(self.le, SIGNAL("tabPressed"),
                     self.update)

    def update(self):
        newtext = str(self.le2.text()) + "tab pressed "
        self.le2.setText(newtext)

####################################################################
class MyLineEdit(QLineEdit):
    def __init__(self, *args):
        QLineEdit.__init__(self, *args)
        
    def event(self, event):
        if (event.type()==QEvent.KeyPress) and (event.key()==Qt.Key_Tab):
            self.emit(SIGNAL("tabPressed"))
            return True

        return QLineEdit.event(self, event)

####################################################################
if __name__ == "__main__": 
    main()

4 Comments — feed icon Comments feed for this post


#1 mk18 commented on 2008-09-08:

good example,thnx


#2 William commented on 2009-06-22:

Thanks, I was trying to reimplement keyPressedEvent and it wasn't working, but this set me straight.


#3 BMG commented on 2009-11-26:

Thank you very much for this very clear and useful example ! It saved me a lot of google-time !


#4 Amy commented on 2011-05-01:

Thanx it's very helpfyl :)

Post a comment

Required
Required, but not displayed
Optional

Format using Markdown. (No HTML.)
  • Code blocks: prefix each line by at least 4 spaces or 1 tab (and a blank line before and after)
  • Code span: surround with backticks
  • Blockquotes: prefix lines to be quoted with >
  • Links: <URL>
  • Links w/ description: [description](URL)
Created with Django | Hosted by Slicehost