Looks like using the QProcess::stateChanged signal (and removing terminate()) works.
It uses QThread::msleep() so its not ideal, but its fine for me.
HWND myHWND;
static BOOL CALLBACK enumWindowCallback(HWND hWnd, LPARAM lparam) {
DWORD pid;
GetWindowThreadProcessId(hWnd, &pid);
if (pid == lparam) {
myHWND = hWnd;
return FALSE;
}
return TRUE;
}
ProgramStarter::ProgramStarter(QObject *parent)
: QObject{parent}
{
QProcess *qpr = new QProcess(this);
connect(qpr, &QProcess::stateChanged, this, [=](QProcess::ProcessState newState){
if (newState == QProcess::Running) {
QThread::msleep(250);
EnumWindows(enumWindowCallback, qpr->processId());
}
});
QString executable = "C:\\path\\to\\program.exe";
qpr->start(executable);
qDebug() << myHWND;
}