You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Qt 5.10.1静态编译版在Windows下调用QProcess打开CMD失败求助

Workaround for Qt 5.8+ Static MinGW32 Build CMD Launch Failure

It sounds like you're hitting a well-documented regression introduced in Qt 5.8 that affects static MinGW32 builds when trying to launch system executables like cmd.exe. This bug breaks the process initialization flow that worked reliably in Qt 5.6, and it manifests across both Windows 7 and 10.

Here are a few proven workarounds to get your terminal opening code working again:

1. Use the Sysnative Path Alias (32-bit on 64-bit Windows)

On 64-bit Windows, 32-bit applications are redirected from System32 to SysWOW64 by default. To bypass this redirection and access the real system executable, use the sysnative alias. Add a check to handle 32-bit Windows systems where this alias doesn't exist:

QString cmdPath;
#ifdef _WIN32
    if (QFile::exists("C:/Windows/sysnative")) {
        cmdPath = "C:/Windows/sysnative/cmd.exe";
    } else {
        cmdPath = "C:/Windows/system32/cmd.exe";
    }
#endif

// Launch the command prompt
QProcess process;
process.start(cmdPath);
process.waitForStarted();

2. Use Native Windows CreateProcess API

If QProcess still misbehaves, bypass it entirely using the Windows native API. This avoids any Qt-specific process handling issues:

#include <windows.h>

void openCmd() {
    STARTUPINFO si = { sizeof(si) };
    PROCESS_INFORMATION pi;
    LPCWSTR cmdPath = L"C:\\Windows\\system32\\cmd.exe";

    if (CreateProcessW(cmdPath, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
        // Clean up handles
        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);
    }
}

3. Backport the Qt Bug Fix (Advanced)

If you're comfortable modifying and rebuilding Qt from source, this bug was addressed in later Qt versions. You can locate the relevant fixes in the qprocess_win.cpp file and backport them to your 5.10.1 static build. This is the most permanent solution but requires more effort.

All these approaches should resolve the issue you're facing, as they either bypass Qt's broken process initialization or work around the system redirection causing the failure.

内容的提问来源于stack exchange,提问作者Alan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:08:24