Solved 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 aQCompleter
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
withQDir::homePath()
but have theQFileSystemModel
watchingQDir::rootPath()
. CallingsetText
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 theQLineEdit
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?
-
@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?
-
@jsulm Excellent! Do you have any ideas for the main problem?
-
@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 withQCompleter
. I ended up with a much nicer experience for the user afterwards. It might not be as efficient (I'm usingQDir::entryList
) but it's not slow enough for the user to notice.