提问者:小点点

用QProcess执行CMD命令,并将其保存在QString中


我想用Qprocess执行cmd命令“WMIC share get name”,然后将命令的结果保存在Qstring变量中。 另外,我想在QMessageBox中显示它,或者。。。我怎么做呢?


共1个答案

匿名用户

您可以为此使用Qprocess。 假设我想执行g++。 示例:

    QProcess p;
    p.setProgram("g++");
    p.setArguments({"-O3", "filename.cpp"});
    p.start();

    // wait for the process to finish executing
    // returns true on success
    if (!p.waitForFinished()) {
       qDebug() << "Failed to execute!!";
       const QString error = p.readAllStandardError();
       if (!error.isEmpty()) {
        qDebug () << "Exit status: " << p.exitStatus() << ", Error: " << error;   
       }
        return;
    }

    // read output
    const QString output = p.readAllStandardOutput();
    qDebug () << output;

    // read error
    const QString error = p.readAllStandardError();
    if (!error.isEmpty()) {
        qDebug () << error;   
    }

    //do whatever you want with output