Completion for QFileSystemModel
-
I would like to add a line edit, which can select files from a file system model - searching files by name. In theory - this code should work:
auto model = new QFileSystemModel(this); auto completer = new QCompleter(model, this); auto home = QDir::homePath(); model->setRootPath(home); completer->setCompletionColumn(QFileSystemModel::FileNameRole); ui->treeView->setModel(model); ui->treeView->setRootIndex(model->index(home)); ui->lineEdit->setCompleter(completer); ui->lineEdit->setFocus();In practice - if I remove the "completion column" I can select a file using pull path. However - I only want completion by filename.
Other alternative I can use - is extract the whole tree in the dir, create a flat list of files - and manually filter this (overriding most of built in Qt classes). I would like to avoid it.
-
I would like to add a line edit, which can select files from a file system model - searching files by name. In theory - this code should work:
auto model = new QFileSystemModel(this); auto completer = new QCompleter(model, this); auto home = QDir::homePath(); model->setRootPath(home); completer->setCompletionColumn(QFileSystemModel::FileNameRole); ui->treeView->setModel(model); ui->treeView->setRootIndex(model->index(home)); ui->lineEdit->setCompleter(completer); ui->lineEdit->setFocus();In practice - if I remove the "completion column" I can select a file using pull path. However - I only want completion by filename.
Other alternative I can use - is extract the whole tree in the dir, create a flat list of files - and manually filter this (overriding most of built in Qt classes). I would like to avoid it.
completer->setCompletionColumn(QFileSystemModel::FileNameRole);For a role like you have you're supposed to use setCompletionRole(int role). You have picked a non-existent column number :)
-
completer->setCompletionColumn(QFileSystemModel::FileNameRole);For a role like you have you're supposed to use setCompletionRole(int role). You have picked a non-existent column number :)
I was under impression that this is the role to use :)
I used also :
completer->setCompletionColumn(Qt::DisplayRoleAnd completion still works on full path (not the displayed text).
-
I was under impression that this is the role to use :)
I used also :
completer->setCompletionColumn(Qt::DisplayRoleAnd completion still works on full path (not the displayed text).
@Diego-Iastrubni
Please just re-read (carefully) what I wrote, you were using the correct role but calling the wrong method for a role. Roles and columns are quite different things. -
@Diego-Iastrubni
Please just re-read (carefully) what I wrote, you were using the correct role but calling the wrong method for a role. Roles and columns are quite different things.Got what you said. Next iteration:
completer->setCompletionRole(QFileSystemModel::FileNameRole); completer->setCompletionRole(Qt::DisplayRole);I tried each one of those - and with each one of them, still completion completion works on path, and not display.
-
Got what you said. Next iteration:
completer->setCompletionRole(QFileSystemModel::FileNameRole); completer->setCompletionRole(Qt::DisplayRole);I tried each one of those - and with each one of them, still completion completion works on path, and not display.
@Diego-Iastrubni
I would expect the first one to be correct. I know no more than that.You might verify how many columns the model has (I don't know), you are currently searching on column #0, make sure that's right.
If you derived from
QFileSystemModeljust so you can override thedata()method, you could put debug in there to verify it is being called withFileNameRole, and what that is returning. -
Adding debug to a derived model .. .was not succesfull. It produced too much unusable debug.
I looked at the code of QCompleter, and saw that it has ... "hack" specific to QFileSystemModel - so my task seems untrivial:
https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/util/qcompleter.cpp?h=dev#n1790
I will probable need to derive a new QCompleter... and bypass lots of functionality. Unless - someone tells me I am going completely off - and there is a simpler way.
-
Adding debug to a derived model .. .was not succesfull. It produced too much unusable debug.
I looked at the code of QCompleter, and saw that it has ... "hack" specific to QFileSystemModel - so my task seems untrivial:
https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/util/qcompleter.cpp?h=dev#n1790
I will probable need to derive a new QCompleter... and bypass lots of functionality. Unless - someone tells me I am going completely off - and there is a simpler way.
@Diego-Iastrubni
Although I agree that code is naughty to be doingQFileSystemModel-specific inQCompleter, I think it just makespathFromIndex()return the full path. I don't know why that should be a problem for you.It would explain
data()being called withQFileSystemModel::FileNameRolea lot.I still think your original code should work with
setCompletionRole(QFileSystemModel::FileNameRole).