[SOLVED] Bring to front window application managed with QProcess



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



  • Hi Bonride

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

    or

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



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



  • 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 toMainWindow::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. 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.



  • It works!
    Thank you very much MuldeR.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.