Results 1 to 18 of 18

Thread: How to raise application from background?

  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.

  13. #12
    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 stampede View Post
    Be careful, in this case this is danger close to "I know what is good for my 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.
    I agree with you with nothing more annoying than an application popping up out of nowhere, but here this is done in case there is already an instance of the application running and the user starting a second instance of that application. In such a case, I want to the first instance to be shown to the user and the second instance closed. In this particular case, I think it's completely fine to go against the default Windows' behaviour.

    Otherwise, about "I know what is good for my user"... well, though my application is going to be used by many people, I will also happen to be one of those people. So, it's not, here, a matter of the programmer not being a user and, yet, thinking s/he knows better than the user. Also, I have done the above in the previous version of my application (which is written in Delphi and therefore only runs on Windows, hence my switching to Qt to have it cross-platform).

  14. #13
    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?

    So, it's not, here, a matter of the programmer not being a user and, yet, thinking s/he knows better than the user.
    As you've said, you are one of many users, and it's impossible to know what is the best for all of them. If I were using your app, I'd prefer notification in taskbar (yes, I'm the client from hell )
    I can accept raise from background if I explicitly click on the icon in explorer (or start via cmd), but only then. If an application steals the focus without my clear permission, then well ... /dev/null awaits
    Sorry for little off topic btw.

  15. #14
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to raise application from background?

    Isn't that what agarny is doing? The application is only popping up if you double click the application on your desktop (or start bar, or wherever).

    What he didn't want is double clicking the icon and the previous instance just flashing on the task bar. To me, that would be unacceptable. If I'm clicking the icon, I want the application to be in the foreground and not wink at me.

  16. #15
    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?

    Ok! I find the working solution for this problem. I describe this in the case somebody want do it.

    First of all there is mentioned article: http://blogs.msdn.com/b/oldnewthing/...0/9435239.aspx. It describes Microsoft point of view. There are two ways to solve this problem: use SetForegroundWindow in second runing process, or use AllowSetForegroundWindow in second runing proces in couple with SetForegroundWindow in first proces. I choose the latter variant because it does not mess in my project. So I do following steps:

    1. In first process (Proc1) open local socket and listen on it.
    2. In second process (Proc2) connect to the local socket Proc1.
    3. In Proc1 write to socket the proces ID.
    4. In Proc2 read proces ID and call AllowSetForegroundWindow.
    5. In Proc2 write list of file names that should be opened in Proc1.
    6. In Proc1 open each file and call SetForegroundWindow.

  17. #16
    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?

    @squidge: You are right, I should read the topic more carefully. I was thinking about programmatically raising up from background in general whole time. My bad.

  18. #17
    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 stampede View Post
    As you've said, you are one of many users, and it's impossible to know what is the best for all of them. If I were using your app, I'd prefer notification in taskbar (yes, I'm the client from hell )
    I can accept raise from background if I explicitly click on the icon in explorer (or start via cmd), but only then. If an application steals the focus without my clear permission, then well ... /dev/null awaits
    Sorry for little off topic btw.
    Well... this is exactly the situation we are talking about here. I mean, having the user explicitly clicking the icon in explorer or start the application from the command line...

  19. #18
    Join Date
    Apr 2012
    Location
    India.
    Posts
    88
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to raise application from background?

    The first solution code provided by agarny is working fine for me, Thanks to agarny for your solution.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.