[Interest] QCompleter, QFileSystemModel and relative paths

Wilhelm wilhelm.meier at fh-kl.de
Thu Dec 27 14:09:29 CET 2012


Hi all,

I try to build a completer for a QPlainTextEdit. The below code-snippet 
is from MyPlainTextEdit::keyPressEvent(...). It uses the actual word 
under cursor as completionPrefix.

If it starts with "/" all runs well.

But using the word as a releative name according to the current dir, I 
can't get it right. I tried the below, but model->mapFromSource(i) 
allways returns an invalid QModelIndex.

Whats wrong here?

       if (!completionPrefix.startsWith(QDir::separator())) {
             QAbstractProxyModel* model =
qobject_cast<QAbstractProxyModel*>(mCompleter->completionModel());
             Q_ASSERT(model);
             QFileSystemModel* fsm =
qobject_cast<QFileSystemModel*>(model->sourceModel());
             if (fsm) {
                 QModelIndex i = fsm->index(QDir::currentPath());
                 QModelIndex vi = model->mapFromSource(i);
                 qDebug() << "fsm" << QDir::currentPath() << i << vi;
                 mCompleter->popup()->setRootIndex(vi);

             }
         }
         else {
             mCompleter->setCompletionPrefix(completionPrefix);

mCompleter->popup()->setCurrentIndex(mCompleter->completionModel()->index(0,
0));
         }
-- 
Wilhelm




More information about the Interest mailing list