简体   繁体   中英

PyQt5 QCompleter how to clear the QLineEdit after auto-completion text was selected

How can I clear a text box (QLineEdit) after an auto completion text was selected using QCompleter? - Nothing is working for me.

Below is a simpler example of my code: it is a simple "echo" console application that gets text commands from QLineEdit (input text box) and writing it to QTextBrowser (output text box). Once the user pressed ENTER on the input text box, the text from that one should be added to the output text box, and the input text box should be cleared (to be ready for the next command).

When entering a new text, that is not in the auto completion options, all works well: the text is added to the output text box and cleared from the input text box. However when the user chooses one of the options from the auto completion options (using the down/up arrows) and then press ENTER, the text is being added to the output text box, but is not removed from the input text box...

Both cases call my callback function (trigger function) which should clear the input text box, using:

self.consoleCommandLineEdit.clear()

but when the auto completion text is chosen by ENTER, the input text box is not cleared... The text is added correctly to the output text box, so my callback function is called properly. It looks like that the Completer is filling my text box after I clear it... How can I disable this behavior and make sure the input text box is cleared? can I remove it??

Below is the entire example. You can try that by typing 'zzz' or any other text that is not in the auto completions list and press ENTER, and then try typing 'a' then go down with the keyboard to 'aaa1' (or any other on the auto completion options) and press ENTER. You will see the input text box is not cleared in this case.

import sys
from PyQt5.QtGui import QStandardItem, QStandardItemModel
from PyQt5.QtWidgets import (
    QApplication,
    QWidget,
    QMainWindow,
    QVBoxLayout,
    QLineEdit,
    QTextBrowser,
    QCompleter,
)


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.setWindowTitle("console")
        self.setGeometry(10, 50, 500, 800)

        # Create text box for input
        self.consoleCommandLineEdit = QLineEdit()
        self.consoleCommandLineEdit.setFixedHeight(25)
        self.consoleCommandLineEdit.editingFinished.connect(self.gotConsoleCommand)

        self.model = QStandardItemModel()
        self.model.appendRow(QStandardItem('aaa1'))
        self.model.appendRow(QStandardItem('aaa2'))
        self.model.appendRow(QStandardItem('aaa3'))
        completer = QCompleter(self.model, self)
        self.consoleCommandLineEdit.setCompleter(completer)

        # Create text box for output
        self.consoleViewer = QTextBrowser(lineWrapMode=QTextBrowser.NoWrap)

        widget = QWidget()
        self.setCentralWidget(widget)
        vlay = QVBoxLayout(widget)
        vlay.addWidget(self.consoleCommandLineEdit)
        vlay.addWidget(self.consoleViewer)

    def gotConsoleCommand(self):
        cmd = self.consoleCommandLineEdit.text()
        self.consoleCommandLineEdit.clear()
        self.consoleViewer.append(cmd)  # add cmd to output box


if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit(app.exec_())

The problem is that the task of the QCompleter is to complete the text of the QLineEdit and that is what it is doing, I explain in detail what is happening: The user selects one of the popup options of the QCompleter, then press the enter key, This causes the popup to close but also sends the event of the enter key to the QLineEdit causing the editingFinished signal to be emitted, your code deletes the text but then the QCompleter adds the selected text.

The solution is that after adding that text you have to clean using a QTimer and the activated QCompleter signal:

    # ...
    completer = QCompleter(self.model, self)
    
    # ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM