Important: Please read the Qt Code of Conduct -

Qt switches focus when QLineEdit with QCompleter is cleared

  • I am using a QMainWindow which opens a QDialog. The QDialog contains a QLineEdit with a QCompleter. The QCompleter works as expected when I type in some string. However, when I remove all characters in the QLineEdit or type in a string for which QCompleter is unable to find a result, the focus switches from the QDialog to the QMainWindow. Does anyone know what is causing this behavior and how I can solve this?

    Edit: I'm running OSX Mavericks in combination with Qt5.2

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Can you post a minimal example that shows the behavior ?

  • Thanks SGaist for your reply. I've simplified my program to a very simple example in which I can reproduce the results

    The file consists of a QMainWindow class and a QDialog class. Both contain QlineEdits, but the QLineEdit in the QDialog makes use of a completer.
    When you type the letter 'a' in the Qdialog, the completer correctly suggests the string "abcd". If you then also type in the letter 'd' (the string becomes "ad"), the focus switches to the line edit in the QMainWindow. I don't know what is causing this behaviour. This also occurs when I remove the letter 'a' after the QCompleter has made a suggestion.

    I hope someone can help.

    @#include <QLineEdit>
    #include <QStringListModel>
    #include <QApplication>
    #include <QMainWindow>
    #include <QDialog>
    #include <QCompleter>
    #include <QStringList>

    int main(int argc, char *argv[])
    //initialize the mainwindow and dialog
    QApplication a(argc, argv);
    QMainWindow mainWindow;;
    QDialog dialog(&mainWindow);

    //create two line edits
    QLineEdit *mainWindowEdit=new QLineEdit(&mainWindow);
    QLineEdit* dialogEdit=new QLineEdit(&dialog);
    QStringListModel model;
    //set text of line edits
    //add Qcompleter with stringlist to dialog line edit
    QStringList list;
    dialogEdit->setCompleter(new QCompleter(&dialog));
    //show both;;
    return a.exec(&#41;;


Log in to reply