带有“ cmd”命令的QProcess不会在命令行窗口中显示 [英] QProcess with 'cmd' command does not result in command-line window

查看:791
本文介绍了带有“ cmd”命令的QProcess不会在命令行窗口中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将代码从MinGW移植到MSVC2013 / MSVC2015,发现一个问题。

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);

当我使用MinGW时,此代码将在命令行窗口中显示。但是,当我使用MSVC2013或MSVC2015时,相同的代码将导致cmd进程在没有任何窗口的后台运行。有什么方法可以使命令行窗口出现?

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?

推荐答案

问题与msvc2015有关,而不与Qt5.8.0有关。有逃脱它的方法。想法是使用CREATE_NEW_CONSOLE标志。

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;
}

这篇关于带有“ cmd”命令的QProcess不会在命令行窗口中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆