QProcess with 'cmd' command does not result in command-line window

落花浮王杯 提交于 2019-12-08 04:11:00

问题


I am porting code from MinGW to MSVC2013/MSVC2015 and found a problem.

QProcess process;
QString program = "cmd.exe";
QStringList arguments = QStringList() << "/K" << "python.exe";
process.startDetached(program, arguments);

When I use MinGW, this code results in command-line window. But when I use MSVC2013 or MSVC2015, the same code results in cmd-process running in background without any windows. Are there any ways to make command-line window appear?


回答1:


The problem was connected with msvc2015, not with Qt5.8.0. There is the way to escape it. The idea is to use CREATE_NEW_CONSOLE flag.

#include <QProcess>
#include <QString>
#include <QStringList>
#include "Windows.h"

class QDetachableProcess 
        : public QProcess {
public:
    QDetachableProcess(QObject *parent = 0) 
        : QProcess(parent) {
    }
    void detach() {
       waitForStarted();
       setProcessState(QProcess::NotRunning);
    }
};

int main(int argc, char *argv[]) {
    QDetachableProcess process;
    QString program = "cmd.exe";
    QStringList arguments = QStringList() << "/K" << "python.exe";
    process.setCreateProcessArgumentsModifier(
                [](QProcess::CreateProcessArguments *args) {
        args->flags |= CREATE_NEW_CONSOLE;
        args->startupInfo->dwFlags &=~ STARTF_USESTDHANDLES;
    });
    process.start(program, arguments);
    process.detach();
    return 0;
}


来源:https://stackoverflow.com/questions/42051405/qprocess-with-cmd-command-does-not-result-in-command-line-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!