Results 1 to 18 of 18

Thread: How to raise application from background?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2010
    Posts
    28
    Thanks
    6
    Thanked 10 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Exclamation How to raise application from background?

    Hi

    I am writting kind of text editor program. So the main feature is "running only one instance" and raising app from background when user invoke it with file name as parameter. Already I have done comunication via local socket (between already running app and new starting app), but I have no idea how to raise it from background when it obtain file name from socket. I use QWidget::raise() and QWidget::activateWindow() but it does not work, the only effect is blinking program icon on task bar.

    In other words I want the same behaviour (raise program from background) as Chrome or any text editor when it is already running as "only one instance" and when user invoke it with file name or URL as parameter.

    System: Windows 7 32bit.

  2. #2
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to raise application from background?

    As you noticed, activateWindow() is not what you need on Windows. Personally, I do the following:
    Qt Code:
    1. // Retrieve your application's window Id
    2.  
    3. WId mwWinId = winId();
    4.  
    5. // Restore your application, should it be minimized
    6.  
    7. if (IsIconic(mwWinId))
    8. SendMessage(mwWinId, WM_SYSCOMMAND, SC_RESTORE, 0);
    9.  
    10. // Bring your application to the foreground
    11.  
    12. DWORD foregroundThreadPId = GetWindowThreadProcessId(GetForegroundWindow(), NULL);
    13. DWORD mwThreadPId = GetWindowThreadProcessId(mwWinId, NULL);
    14.  
    15. if (foregroundThreadPId != mwThreadPId) {
    16. // Your application's thread process Id is not that of the foreground window, so
    17. // attach the foreground thread to your application's, set your application to the
    18. // foreground, and detach the foreground thread from your application's
    19.  
    20. AttachThreadInput(foregroundThreadPId, mwThreadPId, true);
    21.  
    22. SetForegroundWindow(mwWinId);
    23.  
    24. AttachThreadInput(foregroundThreadPId, mwThreadPId, false);
    25. } else {
    26. // Your application's thread process Id is that of the foreground window, so
    27. // just set your application to the foreground
    28.  
    29. SetForegroundWindow(mwWinId);
    30. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to agarny for this useful post:

    JohannesMunk (23rd February 2011)

  4. #3
    Join Date
    Dec 2010
    Posts
    28
    Thanks
    6
    Thanked 10 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Exclamation Re: How to raise application from background?

    Thank you for reply!

    But consider case when my app is not minimized on task bar and not at the foreground. In that case my app will not be raised from background! So this is not complete solution.

  5. #4
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to raise application from background?

    Quote Originally Posted by oficjalne100 View Post
    But consider case when my app is not minimized on task bar and not at the foreground. In that case my app will not be raised from background! So this is not complete solution.
    May I ask you whether you have actually tried the code I posted? For your information, I use that code in my application and it does exactly what you are after (I have even double-checked with my application).

  6. #5
    Join Date
    Dec 2010
    Posts
    28
    Thanks
    6
    Thanked 10 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to raise application from background?

    Ofcourse I copy paste your code in to my app and I was trying it. My test procedure was as follow: 1) run my app from file manager 2) run cmd.exe in the same directory 3) type program name and file name as parameter and hit Enter. Then not minimized app is not raised but only blinking on taskbar. In the same situation Chrome raises properly. My system is Windows 7 32bit.

  7. #6
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to raise application from background?

    Well, I don't know exactly how you are using my code, but I can guarantee you that it works. So, I am assuming you are doing something wrong somewhere. Out of curiosity, you mentioned wanting to run only one instance of your application at any given time. How do you go about that? Are you using QtSingleApplication? I personally do and then make the following connection:
    Qt Code:
    1. QObject::connect(&app, SIGNAL(messageReceived(const QString&)),
    2. &win, SLOT(singleAppMsgRcvd(const QString&)));
    To copy to clipboard, switch view to plain text mode 
    with singleAppMsgRcvd() containing the code I posted, and as I said this works perfectly fine for me, so...

  8. #7
    Join Date
    Dec 2010
    Posts
    28
    Thanks
    6
    Thanked 10 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to raise application from background?

    My app must receive parameters from command line as well run another instance when it is invoke without any parameters. QtSingleApplication is new to me but after quick look at it's documentation I think it is not best solution for me. I create custom Application class with local socket and with signal showMainWindow() which I emit to main window raiseWindow() after obtaining file name from socket. I do exactly the same as it is in demo Browser from Qt demos. And in true I have the same behaviour (this is bad).

  9. #8
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to raise application from background?

    Quote Originally Posted by oficjalne100 View Post
    My app must receive parameters from command line as well run another instance when it is invoke without any parameters. QtSingleApplication is new to me but after quick look at it's documentation I think it is not best solution for me. I create custom Application class with local socket and with signal showMainWindow() which I emit to main window raiseWindow() after obtaining file name from socket. I do exactly the same as it is in demo Browser from Qt demos. And in true I have the same behaviour (this is bad).
    You are not making sense to me, so I am going to leave you to it. Just in case, though, QtSingleApplication inherits from QApplication which can definitely deal with parameters. Anyway, I know it all works fine for me (incl. with parameters), so too bad for you indeed that you can't get things to work.

  10. #9
    Join Date
    Dec 2010
    Posts
    28
    Thanks
    6
    Thanked 10 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Exclamation Re: How to raise application from background?

    There is interesting article which describe this problem clearly: http://blogs.msdn.com/b/oldnewthing/...0/9435239.aspx. It describes clearly, that your solution can't work, or if work then it is windows inconsistence. Any way your code do not make what I want under Windows 7.

  11. #10
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to raise application from background?

    Quote Originally Posted by oficjalne100 View Post
    There is interesting article which describe this problem clearly: http://blogs.msdn.com/b/oldnewthing/...0/9435239.aspx. It describes clearly, that your solution can't work, or if work then it is windows inconsistence. Any way your code do not make what I want under Windows 7.
    Please read that article again and you might (finally!) realise that my solution can and does work. What I, and many other people, do is partly explained by the guy in his article. What is true, though, is that it goes against Windows' preferred behaviour (since Windows XP?), but I know what is good for my application while Microsoft doesn't.

  12. #11
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to raise application from background?

    I know what is good for my application
    Be careful, in this case this is danger close to "I know what is good for the user". IMHO there is nothing more annoying than application that pops at you out of nowhere, so this time I can agree with Microsoft, simple taskbar notification is less intrusive.

Similar Threads

  1. Background image in an application
    By halvors in forum Qt Programming
    Replies: 1
    Last Post: 1st April 2010, 11:27
  2. How to set background of application??
    By kamlesh.sangani in forum Newbie
    Replies: 21
    Last Post: 9th July 2009, 12:12
  3. widget raise!
    By zgulser in forum Qt Programming
    Replies: 3
    Last Post: 27th May 2009, 07:10
  4. Run application in background
    By raulftang in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 24th April 2007, 09:29
  5. Background QT application.
    By Thrantor in forum Qt Programming
    Replies: 4
    Last Post: 12th September 2006, 22:29

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.