Focus bug with QCompleter

Max Mustermann dooriiaufat at
Tue Oct 27 22:40:13 GMT 2020

Hello everyone,

working on an open source project, I encountered a bug that I
surprisingly could find no mention of anywhere:

It's easy to reproduce: In a QLineEdit with a QCompleter, bring up the
popup, then use a shortcut to change the focus. You end up with two
focussed widgets. The single next key stroke will still be processed by
the line with the completer and the popup and ‘a’ cursor will stay
visible there.

My exact input from the GIF:

 1. F
 2. Ctrl+F
 3. Some Os

The app's source code:

    import sys
    from PyQt5.QtGui import QKeySequence
    from PyQt5.QtWidgets import QApplication, QLineEdit, QMainWindow,
    QWidget, QVBoxLayout, QCompleter, QShortcut

    class MainWindow(QMainWindow):

         def __init__(self, *args, **kwargs):
             super(MainWindow, self).__init__(*args, **kwargs)
             box = QWidget()
             layout = QVBoxLayout(box)
             words = ["foo", "bar"]
             completer = QCompleter(words)
             self.line1 = QLineEdit()
             self.line2 = QLineEdit()
             self.shortcut = QShortcut(QKeySequence("Ctrl+F"), self)

    app = QApplication(sys.argv)
    window = MainWindow()

Apparently in that special case, the QLineEdit with the completer
doesn't receive a focusOutEvent.

I could reproduce this bug with PyQt 5.12 to 5.15.1. On 5.9 with Python
3.7, the shortcut doesn't work with an open popup, so the bug isn't

Kind regards


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: cepbhdmkmmnfgfeh.gif
Type: image/gif
Size: 11629 bytes
Desc: not available
URL: <>

More information about the PyQt mailing list