November 26, 2010

blex blex
Lab Rat
192 posts

activateWindow() does not send window to front

 

I use qtsingleapplication solution to ensure that only one instance of the application is running.

When the second instance is started then it sends message to the already running process.

Already running process should bring it’s window to front. How to achieve this?

I used activateWindow() and raise() but it is not a solution (and not a Qt bug – this is the limitation described in the documentation). These calls just “highlight” window in task bar.

I found solution for Windows.

I cannot find solution for Linux. I am interesting in GNOME on Suse Linux (SLED 10).

Does anybody solved this issue?

 Signature 

————————————

Oleksiy Balabay

8 replies

November 26, 2010

lyuts lyuts
Lab Rat
169 posts

Here is how I do it:

  1. showNormal();
  2. raise();
  3. activateWindow();

and my window has Qt::Popup flag set.

 Signature 

I’m a rebel in the S.D.G.

November 26, 2010

blex blex
Lab Rat
192 posts

Thank you, I will try this sequence.

I think that in most cases it works. I will try to describe the case when it doesn’t work.

 Signature 

————————————

Oleksiy Balabay

November 26, 2010

lyuts lyuts
Lab Rat
169 posts

I use this sequence in my timer and I had no problems with it. Every time the timer fired, the timer window popped up as needed. But if you find the use case when it doesn’t work, I will investigate it.

 Signature 

I’m a rebel in the S.D.G.

November 26, 2010

Gerolf Gerolf
Robot Herder
3252 posts

if your main window is in the background and you want to activate it with a qsingleapp, you will have problems bringing the window to the front.
An application can’t pull the focus on windows. and activate and rais will bring it to front in some cases but not in all :-( That’s why we have this HACK:

  1.     // THIS IS A HACK:
  2.     // from QT documentation:
  3.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4.     // void QWidget::activateWindow ()
  5.     // ...
  6.     // On Windows, if you are calling this when the application is not currently
  7.     // the active one then it will not make it the active window. It will change
  8.     // the color of the taskbar entry to indicate that the window has changed in
  9.     // some way. This is because Microsoft do not allow an application to
  10.     // interrupt what the user is currently doing in another application.
  11.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  12.     // This hack does not give the focus to the app but brings it to front so
  13.     // the user sees it.
  14.     ::SetWindowPos(effectiveWinId(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
  15.     ::SetWindowPos(effectiveWinId(), HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
  16.     // HACK END
  17.  
  18.     raise();
  19.     pWidget->show();
  20.     // pWidget->setFocus(); this does not work :-(
  21.     pWidget->activateWindow();
  22.     pWidget->raise();

It always brings the window to the front, but the focus is somewhere in the system :-( In some other app…

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

November 26, 2010

blex blex
Lab Rat
192 posts
Gerolf Reinwardt wrote:
That’s why we have this HACK

Thank you for code.

Sorry, but what OS is the target of this hack? I have solved the problem on Windows with the similar code. I need solution for Linux.

 Signature 

————————————

Oleksiy Balabay

November 26, 2010

Gerolf Gerolf
Robot Herder
3252 posts

That is only for windows, but handles also cases, where rais does sometime not work.
I think, for linux, you have to do something like the win 32 call to SetWindowPos(Toplevel) with X11 calls. I didn’t find a solution that works without platform specific code. If you look at the code of QWidget setting topmost, you could find how it works on linux and try that out…

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

November 26, 2010

blex blex
Lab Rat
192 posts
Gerolf Reinwardt wrote:
If you look at the code of QWidget setting topmost, you could find how it works on linux and try that out…

Thanks. Already done without any success :)

 Signature 

————————————

Oleksiy Balabay

November 27, 2010

blex blex
Lab Rat
192 posts

lyuts wrote:
Here is how I do it:

  1. showNormal();
  2. raise();
  3. activateWindow();

and my window has Qt::Popup flag set.

Thank you for advice. I cannot reproduce bug with this code. Now I am going to handle the application to our QA team and to wait for results :)

 Signature 

————————————

Oleksiy Balabay

 
  ‹‹ Discussion regarding QWaitCondition. When to use?      [Solved] Problem with lupdate. ››

You must log in to post a reply. Not a member yet? Register here!