4

I've got two applications I'm developing using Qt on windows. I want the user to be able to press a button in one application which makes the other application come to the foreground. (The programs communicate using QLocalSocket and named pipes.)

Currently I'm using Qt's QWidget::activateWindow() which occasionally brings the application to the foreground, but most of the time it just highlights the program on the taskbar.

Can someone please tell me how to do this, preferably using Qt although failing that using the WIN32 API would be fine.


Unfortunately, I couldn't find a way to do this only with Qt. I solved it using Chris Becke's suggestion of calling SetForegroundWindow from the currently active application.

1

6 Answers 6

4

Are you sure this is not a debugging issue? The deal is, if an application HAS the foreground, it is allowed to change the foreground.

Clicking a button on window A will give that windows thread foreground activation. If it calls SetForegroundWindow (or equivalent) on the other window, that window WILL be given the foreground.

If, on the other hand, it simply sends a message to the other app, which tries to SetForeground on itself, that will fail. AllowSetForegroundWindow is used in situations where a 'legacy' app needs to be given permission - by a foreground app - to take the foreground. Once again, AllowSet... only works if called from a thread that owns the current active foreground window.

1
  • Thankyou! This advice helped me with a plain Windows Forms app that needed to pass control to another instance.
    – andypaxo
    Sep 28, 2010 at 17:30
4

On top of the QWidget::activateWindow method, you should call QWidget::raise !

This is what is said here.

1
2

I have a similar case.

I have two Qt applications, A and B, which communicate on a socket. I would like to bring a window of application B up, through a button on application A.

I found that sometimes the widget state is not set correctly, so in the event() function of my applicatons B's widget I did the following:

bool MyWidgetB:event ( QEvent * e )
{
    QEvent::Type type = e->type ();

    // Somehow the correct state of window is not getting set,
    // so doing it manually
    if( e->type() == QEvent::Hide)
    {
        this->setWindowState(WindowMinimized);
    }
    else if( e->type() == QEvent::Show )
    {
        this->setWindowState((this->windowState() & ~WindowMinimized) |
                                 WindowActive);
    }
    return QWidget::event(e);
}

I'm sending a command from application A to B. On receiving it, application B calls the following function on itself:

void BringUpWidget(QWidget* pWidget)
{
   pWidget ->showMinimized(); // This is to bring up the window if not minimized
                              // but beneath some other window

   pWidget ->setWindowState(Qt::WindowActive);
   pWidget ->showNormal();
}

This works for me, on Windows XP, with Qt 3.3. My MainWidget is is derived from a QWidget.

I have found this is also working with a widget derived from QMainWindow, but with some issues. Like if some other child windows are open.

For such a case I store the position of the child windows and hide them, then use the BringUpWidget function to bring my MainWindow widget, and then restore the child windows.

1

This is kind of cheesy, but it works for me:

            this->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
            this->show();
            this->setWindowFlags(Qt::FramelessWindowHint);
            this->show();

Or, if you don't have other flags,

            this->setWindowFlags(Qt::WindowStaysOnTopHint);
            this->show();
            this->setWindowFlags(0);
            this->show();

WindowStaysOnTopHint will almost always force the window to the foreground. Afterwards, you don't really want the window to always stay on top, so reset to whatever the previous flags were.

0

I think the APIs you need are AllowSetForegroundWindow() and SetForegroundWindow(). I don't know what the equivalent Qt calls are.

0

Use showNormal() to go from an iconified state to a visible state.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.