Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Update the QCompleter when calling QLineEdit::setText



  • I'm using a QLineEdit to let the user choose a directory (there's also a button to show a file dialog). I'm initializing a QCompleter like this:

    dirBox = new QLineEdit{this};
    auto *completer = new QCompleter{dirBox};
    auto *model = new QFileSystemModel{completer};
    model->setRootPath(QDir::rootPath());
    completer->setModel(model);
    dirBox->setCompleter(completer);
    dirBox->setText(QDir::homePath());
    

    The problem I'm having is that I want to initialize the QLineEdit with QDir::homePath() but have the QFileSystemModel watching QDir::rootPath(). Calling setText doesn't "update the completer". So if the user appends /De to their home directory, the completer doesn't show them /Desktop. The user must delete the contents of the QLineEdit and start again from the root if they want auto completion.

    I could simply do this:

    model->setRootPath(QDir::homePath());
    

    ...but then the model is no longer watching the root. If there are any changes outside of the home directory, the model won't know about them.

    It seems like I can't have my cake and eat it too!


    As an aside, is there a way to make the completer only show directories and not files?


  • Qt Champions 2019

    @Kerndog73 said in Update the QCompleter when calling QLineEdit::setText:

    As an aside, is there a way to make the completer only show directories and not files?

    https://doc.qt.io/qt-5/qfilesystemmodel.html#setFilter



  • @jsulm Excellent! Do you have any ideas for the main problem?


  • Qt Champions 2019

    @Kerndog73 said in Update the QCompleter when calling QLineEdit::setText:

    Do you have any ideas for the main problem?

    No. I would actually expect this to work as home path is under root.



  • For anyone who's curious, I ended up subclassing QLineEdit and implementing the completion logic myself. The problem discussed in this topic was not the only problem I had with QCompleter. I ended up with a much nicer experience for the user afterwards. It might not be as efficient (I'm using QDir::entryList) but it's not slow enough for the user to notice.


Log in to reply