不放自己的工程了,找到一个官网测试的案例 。目前反应的是5.7.1可以使用,5.8以后,5.12等版本反应不再支持弹出窗口
#include
#include
#include
void startProcess()
{
QProcess p;
p.startDetached("C:\\WINDOWS\\system32\\cmd.exe",
QStringList(),
"C:\\Users");
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPushButton pb("Open Terminal");
pb.show();
pb.connect(&pb, &QPushButton::clicked,
[] () {startProcess();});
return a.exec();
}
还有一种情况
Process.start(“cmd.exe”)跳不出cmd界面
Process.start(“regedit.exe”),Process.start(“notepad.exe”)等能跳出注册表界面
解决方法:Qt5.12 https://github.com/Lxxing/BasicSytax.git QT目录
QStringList arguments;
arguments << "/K";
arguments << "title";
arguments << "child process";
QProcess *myProcess = new QProcess(this);
//QString program = "D:/Work/Code/VsProj/BasicSytax/Qt/QtProcess/x64/Debug/ConsoleTest.exe";
//QString program = "C:/Windows/System32/notepad.exe";
QString program = "C:/Windows/System32/cmd.exe";
myProcess->setCreateProcessArgumentsModifier([](QProcess::CreateProcessArguments *args)
{
//args->startupInfo->wShowWindow = SW_SHOWMAXIMIZED;//此成员变量设置此有效,QT5.12
args->flags |= CREATE_NEW_CONSOLE;
args->startupInfo->dwFlags &= ~STARTF_USESTDHANDLES;
args->startupInfo->dwFlags |= STARTF_USEFILLATTRIBUTE;
args->startupInfo->dwFillAttribute = BACKGROUND_BLUE | FOREGROUND_RED
| FOREGROUND_INTENSITY;
//args->startupInfo->dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
}
);
myProcess->start(program, arguments);
相关知识解析:
使用了Windows函数参数
BOOL creationResult;
creationResult = CreateProcess(
NULL, // No module name (use command line)
cmdLine, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP, // creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&startupInfo, // Pointer to STARTUPINFO structure
&processInformation); // Pointer to PROCESS_INFORMATION structure
typedef struct _STARTUPINFOA {
DWORD cb;
LPSTR lpReserved;
LPSTR lpDesktop;
LPSTR lpTitle;
DWORD dwX;
DWORD dwY;
DWORD dwXSize;
DWORD dwYSize;
DWORD dwXCountChars;
DWORD dwYCountChars;
DWORD dwFillAttribute;
DWORD dwFlags;
WORD wShowWindow;
WORD cbReserved2;
LPBYTE lpReserved2;
HANDLE hStdInput;
HANDLE hStdOutput;
HANDLE hStdError;
} STARTUPINFOA, *LPSTARTUPINFOA;
dwFlags
一个位域,确定在进程创建窗口时是否使用某些 STARTUPINFO成员。该成员可以是以下值中的一个或多个。
CREATE_NEW_CONSOLE
0x00000010
新进程具有一个新的控制台,而不是继承其父级的控制台(默认)。有关更多信息,请参见创建控制台。
该标志不能与DETACHED_PROCESS一起使用。
dwFillAttribute
如果dwFlags指定STARTF_USEFILLATTRIBUTE,则如果在控制台应用程序中创建了新的控制台窗口,则该成员是初始文本和背景色。否则,将忽略此成员。
该值可以是以下值的任意组合:FOREGROUND_BLUE,FOREGROUND_GREEN,FOREGROUND_RED,FOREGROUND_INTENSITY,Background_BLUE,Background_GREEN,Background_RED和Background_INTENSITY。例如,以下值的组合在白色背景上产生红色文本:
FOREGROUND_RED| BACKGROUND_RED| BACKGROUND_GREEN| BACKGROUND_BLUE