Search code examples
c++qtqt6qtcore

QProcess doesn't terminate the program


QProcess pro;
pro.start("Notepad");
QTimer::singleShot(3000, &pro, &QProcess::terminate);

After 3 seconds it must terminate my notepad.exe but it won't I have no idea what's the problem

I changed it with (kill) but it still won't work


Solution

  • By design (OS-dependant) running a command without full path is an equivalent of system function in POSIX and, by its implementation it doesn't have this granularity of control on created processes, because there was a shell process between your application and one you meant to execute. As notepad.exe is a GUI application, it immediately returns control to shell, which is terminated afterward, a Windows-specific behaviour of program's stub code. For all purposes your QProcess is already stopped.

    Qt had in past special behaviour for Windows, where command are passed to cmd.exe. The rules regarding to running, passing arguments to and terminating processes change from program to program, most notable would be cmd.exe itself. Behaviour still vary with newfangled startCommand member function.

    You have to either find the true location of notepad.exe, e.g. via QStandardPaths::locate (introduced in Qt 5.0) or ask user to configure it.