Search code examples
c++qtsystemqfiledialog

C++ QT QFileDialog does not close when using system() in triggered action


void OBJ_Loader::on_actionOpen_triggered()
{
QString filename = QFileDialog::getOpenFileName(this, tr("Open a File"));
if (!filename.isEmpty()) {
    filepath=filename.toUtf8().constData();

    command.append(filepath);
    int TempNumOne=command.size();
    for (int a=0;a<=TempNumOne;a++) { //get letters to a char list so it can be used by system();
                cmd[a]=command[a];
    }
    openfile=true;

    if (openfile) {
        openfile=false;
        system(cmd);
    }
}

}

When the system(cmd); is called the QFileDialog window does not close till the system command finishes. I would like to know if I can close the search window after clicking open.


Solution

  • The system function blocks the event loop: the user interaction requires the event loop to run, and it runs when your code isn't running. Since the system invocation is in your code, you can't simply have it block your process. You need to use QProcess as it has an asynchronous interface. This answer provides a complete example of one process calling itself -- all done from a single executable.