Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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;
    mainWindow.show();
    QDialog dialog(&mainWindow);

    //create two line edits
    QLineEdit *mainWindowEdit=new QLineEdit(&mainWindow);
    QLineEdit* dialogEdit=new QLineEdit(&dialog);
    QStringListModel model;
    mainWindow.setCentralWidget(mainWindowEdit);
    
    //set text of line edits
    mainWindowEdit->setText("MainWindowLineEdit");
    dialogEdit->setText("");
    
    //add Qcompleter with stringlist to dialog line edit
    QStringList list;
    list<<"abcd"<<"bcd"<<"cde";
    dialogEdit->setCompleter(new QCompleter(&dialog));
    model.setStringList(list);
    dialogEdit->completer()->setModel(&model);
    dialogEdit->completer()->setCompletionMode(QCompleter::PopupCompletion);
    dialogEdit->completer()->setCaseSensitivity(Qt::CaseInsensitive);
    
    //show both
    mainWindow.show();
    dialog.show();
    
    return a.exec(&#41;;
    

    }@


Log in to reply