Tutorial :Active windows in Windows and QWidget::activateWindow()


The Qt documentation for QWidget::activateWindow() states:

On Windows, if you are calling this when the application is not currently the active one then it will not make it the active window. It will change the color of the taskbar entry to indicate that the window has changed in some way. This is because Microsoft does not allow an application to interrupt what the user is currently doing in another application.

However, Skype appears to defy this rule. If Skype is running but is not the active application, I can launch it from the start menu and it brings the existing instance to the foreground, activates it and grabs input focus.

And how can I do this?


(NOTE: This is specific to how QtSingleApplication works)

The solution is stupidly simple for my issue. Simply call AllowSetForegroundWindow(ASF_ANY); at the beginning of the application, and the original process will thus be allowed to bring itself to the foreground by use of SetForegroundWindow(). No strange hacks, just one line of code to add and no need to modify QtSingleApplication either.


I don't think you can do it reliably with the Qt API alone.

There are multiple solutions for windows. E.g. here, and here, and here.

The method I've used before is to declare a shared memory section, and write the application's window handle there. Later, when a second instance of your program is started, you can find the window handle of the first and activate it.

I don't think you have the issue of windows preventing you from doing this in this case, because your second instance is the active application, so it is allowed to 'pass focus' to other windows.


Use Single Application in Qt Solutions

For some applications it is useful or even critical that they are started only once by any user. Future attempts to start the application should activate any already running instance, and possibly perform requested actions, e.g. loading a file, in that instance.


you can set the setWindowOpacity from 0 to 1 .the only thing is you may open it all the time

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »