Qt Execute external program

前端 未结 4 817
感动是毒
感动是毒 2020-12-08 19:53

I want to start an external program out of my QT-Programm. The only working solution was:

system(\"start explorer.exe\");

But it is only wo

相关标签:
4条回答
  • 2020-12-08 20:33

    Just use QProcess::startDetached; it's static and you don't need to worry about waiting for it to finish or allocating things on the heap or anything like that:

    QProcess::startDetached(QDir::homepath + "/file.exe");
    

    It's the detached counterpart to QProcess::execute.

    0 讨论(0)
  • 2020-12-08 20:37

    QDir::homePath doesn't end with separator. Valid path to your exe

    QString file = QDir::homePath + QDir::separator + "file.exe";
    
    0 讨论(0)
  • 2020-12-08 20:52

    If your process object is a variable on the stack (e.g. in a method), the code wouldn't work as expected because the process you've already started will be killed in the destructor of QProcess, when the method finishes.

    void MyClass::myMethod()
    {
        QProcess process;
        QString file = QDir::homepath + "file.exe";
        process.start(file);
    }
    

    You should instead allocate the QProcess object on the heap like that:

    QProcess *process = new QProcess(this);
    QString file = QDir::homepath + "/file.exe";
    process->start(file);
    
    0 讨论(0)
  • 2020-12-08 20:54

    If you want your program to wait while the process is executing, you can use

    QProcess::execute(file);
    

    instead of

    QProcess process;
    process.start(file);
    
    0 讨论(0)
提交回复
热议问题