0

I have a dialog where user selects file(s). I added QCompleter to the line edit, which automatically suggests next file name:

image description

However if user clicks on file, the suggestions disappear:

image description

I want them to reappear if directory is selected and display the files in that directory. I tried to do this inside the QLineEdit::textChanged signal. I connected it to such slot:

void ImportFromExcelDialog::pathChanged( const QString& path )
    if(path.length()>0) {
        QFileInfo info(path);
        if( info.exists() && info.isFile() && info.isReadable() ) {
            // File selected, do stuff with it
        }
        else {
            // If a directory
            if((info.exists() && info.isDir())) {
                if(!path.endsWith("/"))
                    ui->fileLineEdit->setText(path + "/");
                // Assume QCompleter* completer_; which is set in constructor
                if(completer_!=nullptr)
                    completer_->complete();
            }
        }
    }
}

The problem is that calling complete() shows the old list of files, the one for parent directory:

image description

I can click telemetrie as many times as I want and the display won't change.

So how to force QCompleter to reappear and handle the new value of the text field?

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • Try to create a `QTimer`, connect it to `complete()` slot and start it with a small delay. – Pavel Strakhov Aug 25 '16 at 12:58
  • Or better with a zero delay. It just puts an execution of the slot at the end of the event queue and it doesn't look like a hack that much. – Tomas Aug 25 '16 at 13:42
  • @Tomas Well and is it possible to just call slot asynchronously like this? Something like Java's `SwingUtilities.invokeLater` or JavaScript's Node.js `process.nextTick`? Because using timer is still a hack to do just this... – Tomáš Zato Aug 25 '16 at 13:56
  • 1
    Using the `QTimer` with the zero delay is in my opinion a valid way how to execute some code after all queued events are processed. But if you don't like it, you can also use the `QMetaObject::invokeMethod(completer_, "complete", Qt::QueuedConnection);`. Or emit a signal connected to the `complete()` slot with the `Qt::QueuedConnection`. But anyway, does it solve your problem? – Tomas Aug 25 '16 at 14:26

0 Answers0