Focus bug with QCompleter

Max Mustermann dooriiaufat at web.de
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()
             self.setCentralWidget(box)
             layout = QVBoxLayout(box)
             words = ["foo", "bar"]
             completer = QCompleter(words)
             self.line1 = QLineEdit()
             self.line1.setCompleter(completer)
             self.line2 = QLineEdit()
             layout.addWidget(self.line1)
             layout.addWidget(self.line2)
             self.shortcut = QShortcut(QKeySequence("Ctrl+F"), self)
             self.shortcut.activated.connect(self.line2.setFocus)

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


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
reproducable.



Kind regards

Rumo

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20201027/be2feb7c/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: cepbhdmkmmnfgfeh.gif
Type: image/gif
Size: 11629 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20201027/be2feb7c/attachment-0001.gif>


More information about the PyQt mailing list