簡體   English   中英

使用 QProcess 執行 CMD 命令並將其保存在 QString 中

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

我想用QProcess執行 cmd 命令"wmic share get name" ,然后將命令的結果保存在QString變量中。 此外,我想在QMessageBox中顯示它或者......我該怎么做?

您可以為此使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM