3

I have a simple win32 application that uses the createProcess method to call Qt application.

The problem is that I like to bring the Qt application to the foreground.

Who is responsible for this? The parent win32 app or the Qt application?

Mariana
  • 1
  • 2
  • 11
user63898
  • 29,839
  • 85
  • 272
  • 514

3 Answers3

3

The application that currently has the foreground focus is the only one that is allowed to change the foreground focus. You need to use the SetForegroundWindow function to exercise this right.

The "Remarks" section in the documentation contains an applicable list of restrictions:

The system restricts which processes can set the foreground window. A process can set the foreground window only if one of the following conditions is true:

  • The process is the foreground process.
  • The process was started by the foreground process.
  • The process received the last input event.
  • There is no foreground process.
  • The foreground process is being debugged.
  • The foreground is not locked (see LockSetForegroundWindow).
  • The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
  • No menus are active.

An application cannot force a window to the foreground while the user is working with another window. Instead, Windows flashes the taskbar button of the window to notify the user.

The real question is why you need to do this at all. Changing the foreground application is very likely to get you into trouble, either with all of the restrictions that Windows places on it, or with your users. It's a very user-hostile action, which is one of the reasons why Windows has tightened up the restrictions on it in recent years.

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Isn't there a Qt way? My problem: when users press on settings a new window opens but if they don't observe it and press again on settings the settings window will stay in back, I want to say "hey, I'm here. Here, I popped back in front for you". A solution would be to make setVisibile(false) and then setVisible(true) but I don't like this solution, it should just pop up back in front without closing and opening it again. I can't use Windows API cause the application is cross-platform, don't want to brake that. – Lilian A. Moraru Feb 15 '12 at 12:33
  • @Moraru: Yes, that is relatively simple because the settings window is created by *your own* application. The issue here is trying to activate windows from *other* applications (processes). Windows tries to block that because it used to be abused so badly by developers. – Cody Gray - on strike Feb 15 '12 at 21:11
  • Setting the foreground window is a completely valid action, however, if you are doing it in response to a user-initiated action (click, hot-key) where that is the behavior that the user expects to occur. – Craig Tullis May 23 '13 at 21:35
  • There's a difference between setting the foreground *window* and changing the foreground *application*. – Cody Gray - on strike May 23 '13 at 23:25
2

Get the window handle of the Qt application and call SetForegroundWindow

http://msdn.microsoft.com/en-us/library/ms633539.aspx

Crypth
  • 1,576
  • 18
  • 32
0

You probably want to do it from the parent process. The cleanest/most dependable way to use SetForegroundWindow is to call it from the process that's currently in the foreground.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111