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. [SOLVED] Bring to front window application managed with QProcess
QtWS25 Last Chance

[SOLVED] Bring to front window application managed with QProcess

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 6.7k 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.
  • B Offline
    B Offline
    Bonride
    wrote on 29 Jul 2013, 07:58 last edited by
    #1

    Hello, I'm developing a Qt desktop app. This app executes another external app (assistant) managed with QProcess. But when I push the button that shows the assistant, that window does not appear in front of the mainwindow.
    I've tryed with:

    • In mainwindow.h:

    @public slots:
    void on_actionAssistant_triggered();

    private:
    QProcess assistantProcess;@

    • In mainwindow.cpp:

    @void MainWindow::on_actionAssistant_triggered()

    {
    if(!assistantProcess.isOpen())
    {
    const QStringList args = getAssistantProcessArguments();

        assistantProcess.start(QLatin1String("assistant"), args);
    
        if (!assistantProcess.waitForStarted())
        {
            return;
        }
    }
    
    QByteArray byteArray;
    byteArray.append("show contents;");
    assistantProcess.write(byteArray);
    ...@
    

    But it does not work. Any suggestion?
    Thanks in advance.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vittalonline
      wrote on 29 Jul 2013, 11:19 last edited by
      #2

      Hi Bonride

      use this flag in mainwindow
      setWindowFlags(windowFlags()|Qt::WindowStaysOnBottomHint);

      or

      setWindowFlags(windowFlags()|Qt::WindowStaysOnTopHint);
      in external app

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MuldeR
        wrote on 29 Jul 2013, 15:14 last edited by
        #3

        I don't think he want's the new process' window to permanently stay or on top, nor his "main" process' window to permanently stay on bottom.

        --

        I'd rather do:
        @unsigned long childPID= assistantProcess.pid()->dwProcessId;
        EnumWindows(focusChildProcWindow, reinterpret_cast<LPARAM>(&childPID));@

        Using a callback function like this:
        @static BOOL CALLBACK focusChildProcWindow(HWND hwnd, LPARAM lParam)
        {
        DWORD processId = reinterpret_cast<WORD>(lParam);
        DWORD windowProcessId = NULL;
        GetWindowThreadProcessId(hwnd, &windowProcessId);
        if(windowProcessId == processId)
        {
        SwitchToThisWindow(hwnd, TRUE);
        SetForegroundWindow(hwnd);
        return FALSE;
        }

        return TRUE;
        }@

        This will enumerate all windows and bring the desired window (the window belonging to your child process) to the front.

        My OpenSource software at: http://muldersoft.com/

        Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

        Go visit the coop: http://youtu.be/Jay...

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Bonride
          wrote on 30 Jul 2013, 15:57 last edited by
          #4

          Thank you both for answering, I really needed is what has answered MuldeR, but I get an error. Do not quite understand the code so i do not understand the error. I put the error text below (the first two are warning):

          In function 'BOOL focusChildProcWindow(HWND, LPARAM)':
          warning: converting to non-pointer type 'DWORD {aka long unsigned int}' from NULL [-Wconversion-null]

          At global scope:
          warning: 'BOOL focusChildProcWindow(HWND, LPARAM)' defined but not used [-Wunused-function]

          In function ZN10MainWindow34on_actionNeuralAssistant_triggeredEv':* error: undefined reference to MainWindow::focusChildProcWindow(HWND__, long)@8'
          ld returned 1 exit status

          This is what I've done:

          In mainwindow.h:
          @static BOOL CALLBACK focusChildProcWindow(HWND hwnd, LPARAM lParam);@

          In mainwindow.cpp:
          @static BOOL CALLBACK focusChildProcWindow(HWND hwnd, LPARAM lParam)
          {
          DWORD processId = reinterpret_cast<WORD>(lParam);
          DWORD windowProcessId = NULL;
          GetWindowThreadProcessId(hwnd, &windowProcessId);
          if(windowProcessId == processId)
          {
          SwitchToThisWindow(hwnd, TRUE);
          SetForegroundWindow(hwnd);
          return FALSE;
          }

          return TRUE;
          }

          void MainWindow::on_actionNeuralAssistant_triggered()
          {
          unsigned long childPID= assistantProcess.pid()->dwProcessId;

          EnumWindows(focusChildProcWindow, reinterpret_cast<LPARAM>(&childPID));
          

          ...
          }@

          Regards

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MuldeR
            wrote on 30 Jul 2013, 16:04 last edited by
            #5
            1. Remove function declaration of focusChildProcWindow() from your .h file. You only need that function in the same .cpp file where the function is defined, so no need to add a forward declaration to the "public" header file.

            2. You can ignore the type-cast warning for now ;)

            3. Not quite sure about the linker error, but probably will go away when you fix the first issue.

            My OpenSource software at: http://muldersoft.com/

            Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

            Go visit the coop: http://youtu.be/Jay...

            1 Reply Last reply
            0
            • B Offline
              B Offline
              Bonride
              wrote on 30 Jul 2013, 16:42 last edited by
              #6

              It works!
              Thank you very much MuldeR.

              1 Reply Last reply
              0

              3/6

              29 Jul 2013, 15:14

              • Login

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