Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QT Terminate Browser Process

QT Terminate Browser Process

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 699 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dusk Le
    wrote on 3 Nov 2021, 08:47 last edited by
    #1

    I have a task that we need to open and close a specific Window Browser by the QT app.
    Do you have any solution? Help me, please.

    J 1 Reply Last reply 3 Nov 2021, 08:49
    0
    • D Dusk Le
      3 Nov 2021, 08:47

      I have a task that we need to open and close a specific Window Browser by the QT app.
      Do you have any solution? Help me, please.

      J Offline
      J Offline
      JonB
      wrote on 3 Nov 2021, 08:49 last edited by
      #2

      @Dusk-Le
      What is/do you mean by "a specific Window Browser" that you "open and close", this is not clear.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Dusk Le
        wrote on 3 Nov 2021, 10:03 last edited by
        #3

        Example: I open a new window browser and close this window browser after 3 seconds via the QT app. Is that feasible?

        J 1 Reply Last reply 3 Nov 2021, 10:11
        0
        • D Dusk Le
          3 Nov 2021, 10:03

          Example: I open a new window browser and close this window browser after 3 seconds via the QT app. Is that feasible?

          J Offline
          J Offline
          JonB
          wrote on 3 Nov 2021, 10:11 last edited by JonB 11 Mar 2021, 10:12
          #4

          @Dusk-Le
          Probably/possibly, but what is "a new window browser"? I have no idea. What code do you write to do this? Then we can tell you whether/how to "close it".

          1 Reply Last reply
          0
          • D Offline
            D Offline
            Dusk Le
            wrote on 3 Nov 2021, 10:21 last edited by
            #5

            #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.

            J 1 Reply Last reply 3 Nov 2021, 10:33
            0
            • D Dusk Le
              3 Nov 2021, 10:21

              #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.

              J Offline
              J Offline
              JonB
              wrote on 3 Nov 2021, 10:33 last edited by
              #6

              @Dusk-Le
              This is the first time you have shown you are running a QProcess! That is what I needed to know.

              So you can use QProcess::terminate() for choice, if that does not work then QProcess::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 external Chrome.exe process.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                Dusk Le
                wrote on 3 Nov 2021, 10:42 last edited by
                #7

                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.

                J 1 Reply Last reply 3 Nov 2021, 10:58
                0
                • D Dusk Le
                  3 Nov 2021, 10:42

                  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.

                  J Offline
                  J Offline
                  JonB
                  wrote on 3 Nov 2021, 10:58 last edited by JonB 11 Mar 2021, 11:01
                  #8

                  @Dusk-Le
                  You are already spawning the Chrome.exe as a child of your Qt app. If QProcess::kill() does not work you have a problem. Quite possibly because like I suggested the original QProcess 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 or kill one chrome window, to see how difficult/easy this is.

                  Then if you don't kind a better way, start with the following:

                  1. Keep your process.start(). You might want to add qDebug() << process.processId(); after it, for use below.
                  2. Comment out msleep/close/kill().
                  3. Let the Chrome run up its window/page/tab.
                  4. Go into Windows Task Manager.
                  5. 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.)
                  6. By experimenting with End Process them, find which one (or ones?) you need to terminate to achieve the right one for your window.
                  7. Then use Windows EnumProcesses etc. to determine how you can find that desired process(es), and Windows calls to kill it.
                  1 Reply Last reply
                  1

                  3/8

                  3 Nov 2021, 10:03

                  5 unread
                  • Login

                  • Login or register to search.
                  3 out of 8
                  • First post
                    3/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved