简体   繁体   English

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

[英]Execute CMD Command with QProcess and Save it in QString

I wanted to execute the cmd command "wmic share get name" with QProcess and then save the result of the command in the QString variable.我想用QProcess执行 cmd 命令"wmic share get name" ,然后将命令的结果保存在QString变量中。 In further, I wanted to show it in QMessageBox or... How can I do that?此外,我想在QMessageBox中显示它或者......我该怎么做?

You can use QProcess for this.您可以为此使用QProcess Let's say I want to execute g++ .假设我想执行g++ Example:例子:

    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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM