QT Terminate Browser Process
-
#include <QCoreApplication>
#include "QProcess"
#include "QThread"int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QProcess process;
process.start("C:/Program Files/Google/Chrome/Application/Chrome.exe", QStringList() << "--new-window" << "google.com");
QThread::msleep(3000);
process.close(); //cannot close this browser process in here
return a.exec();
}You can see this code, I can open but I cannot close it. I cannot find out the solution.
-
@Dusk-Le
This is the first time you have shown you are running aQProcess
! That is what I needed to know.So you can use
QProcess::terminate()
for choice, if that does not work thenQProcess::kill()
.However these --- particularly
kill()
--- are "not nice to do".Also there is no guarantee that the process initiated from Qt via
QProcess
is actually the same process as opens that window. Chrome may have internals which create its own processes, or it might use a single process which has multiple windows open. I don't know, you'll have to try it and see.If it turns out you need to find the required process to kill, e.g. by its "name" perhaps for that window, you would have to look at Windows code for
EnumProcesses
etc.Finally, there might be an alternative. Instead of asking an external Chrome process to run to display this window/page, Qt has
QtWebEngine
(webenginewidgets
). That is a Qt widget using the chromium engine to display web pages. If you changed over to that you would have more control than via running an externalChrome.exe
process. -
This job needs to run external Chrome.exe. That is the big problem.
I try to use kill() or terminate(), but it doesn't work too.
Another person told me that making the browser process is the child of the cmd process. Kill cmd process will kill browser process too. But I don't know how to try this solution. -
@Dusk-Le
You are already spawning theChrome.exe
as a child of your Qt app. IfQProcess::kill()
does not work you have a problem. Quite possibly because like I suggested the originalQProcess
you spawn is not kept as the process for the resulting opened window, due to Chrome internals.Start by Googling for, say,
kill one chrome process
orkill one chrome window
, to see how difficult/easy this is.Then if you don't kind a better way, start with the following:
- Keep your
process.start()
. You might want to addqDebug() << process.processId();
after it, for use below. - Comment out
msleep
/close
/kill()
. - Let the Chrome run up its window/page/tab.
- Go into Windows Task Manager.
- Look through all the (scary number of!) running Chrome processes. (Hint: You may want to View > Select Columns... > find and check the Command Line column.)
- By experimenting with End Process them, find which one (or ones?) you need to terminate to achieve the right one for your window.
- Then use Windows
EnumProcesses
etc. to determine how you can find that desired process(es), and Windows calls to kill it.
- Keep your