Results 1 to 11 of 11

Thread: find window from another application

  1. #1
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default find window from another application

    hello. i would like to know if there is any way to get access to the window of another application. i want to prevent my application from being started twice, if that happens i'd like to find the main window of another application and bring it to the front. i can achieve this with WinAPI FindWindow, but i'd like it to be platform-independent.

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: find window from another application

    you described is very platform dependent.

    however, If you need it do avoid multiple instances of your app you can do it in different way.
    For example using QtSingleApplication from Qt-Solutions
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: find window from another application

    you described is very platform dependent
    what is platform dependent? preventing from starting two instances or bringing a window to the front?

    i heard about QtSingleApplication from Qt-Solutions, but not sure whether i can freely use it in the closed-source app. here someone tells that Qt Enterprise license is needed to use it.

  4. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: find window from another application

    Quote Originally Posted by mentalmushroom View Post
    what is platform dependent?
    To find Window for other application.

    The link you posted is very old.
    For Qt-Solution Licensing read here. Is released with BSD licence
    A camel can go 14 days without drink,
    I can't!!!

  5. #5
    Join Date
    Sep 2008
    Location
    Poland
    Posts
    80
    Thanks
    4
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: find window from another application

    there is a better and easier way to archieve that.Inside of your main.cpp add:
    Qt Code:
    1. //do not allow to running multiple instances of the application
    2. QSharedMemory mem("SomeUniquekeyNameThere");
    3. if(!mem.create(1))
    4. {
    5. QMessageBox::critical(0,"Instance detected!","The application is already running!\nApplication terminating...","Ok");
    6. exit(0);
    7. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: find window from another application

    wouldn't you like to share it with us?

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

    Default Re: find window from another application

    I wouldn't say that was a better way. It provides no method of notifying the existing application that another instance is trying to start up, provides no way of passing parameters to a already running instance, doesn't cause the already running instance to bring itself to the front, etc.

    Unless you have more code you would like to share?

  8. #8
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: find window from another application

    I have a problem of building qtsingleapplication. I ran "configure.bat" with "-library" switch, then called qmake and opened "qtsingleapplication.pro" with Visual Studio Qt Add-in, and when I tried to build it, I got the following error:
    Error 2 error C2491: 'QtSingleApplication::staticMetaObject' : definition of dllimport static data member not allowed z:\qt-solutions-qt-solutions-master\qt-solutions-qt-solutions\qtsingleapplication\buildlib\release\moc _qtsingleapplication.cpp 51 QtSolutions_SingleApplication-head

    But one project (console.pro) was successfully built.

  9. #9
    Join Date
    Sep 2008
    Location
    Poland
    Posts
    80
    Thanks
    4
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: find window from another application

    Quote Originally Posted by squidge View Post
    I wouldn't say that was a better way. It provides no method of notifying the existing application that another instance is trying to start up, provides no way of passing parameters to a already running instance, doesn't cause the already running instance to bring itself to the front, etc.
    True,but aboves are not a mentalmushroom's goal.I've never needed such detailed solution,but if I have I'd interest in QSessionManager.

    @mentalmushroom
    Show us all the code please.
    Last edited by MasterBLB; 3rd May 2011 at 14:49.

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

    Default Re: find window from another application

    Its not his complete goal, but I assumed he wanted to at least bring the window of the running instance to the front and do it in a platform independant way. Once you can notify the existing application you can easily ask that application to bring itself to the front (rather than trying to find its windows and force it to the front).

  11. #11
    Join Date
    Sep 2008
    Location
    Poland
    Posts
    80
    Thanks
    4
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: find window from another application

    Session manager seems not to be the right tool to do so,according to what's written in the assistant.
    Well,real inter-process comunnication (I'm mean some that can withstand all conditions mentioned by squidge) provides DBus,QCOPChannel and TCP/IP.But according to mentalmushroom's description of OS something aviable for windows is needed,so it left only TCP/IP for consideration.Well,my idea then is somesuch:

    Qt Code:
    1. thats still shoud be:
    2. //do not allow to running multiple instances of the application
    3. QSharedMemory mem("SomeUniquekeyNameThere");
    4. if(!mem.create(1))
    5. {
    6. QMessageBox::critical(0,"Instance detected!","The application is already running!\nApplication terminating...","Ok");
    7.  
    8. //pseudocode now
    9. send UDP datagram with let's say "SomeUniquekeyNameThere"
    10. anyway,using datagram you may invent many cases for many purposes,not only activating currently running app's window.
    11.  
    12. exit(0);
    13. }
    14. else
    15. {
    16. //pseudocode now
    17. create QUdpSocket on 127.0.0.1 and connect it's readyRead signal to some slot in your main window class
    18. }
    19.  
    20. yourMainWindowClass::yourSlotReadingUDPDatagrams
    21. {
    22. //you've received SomeUniquekeyNameThere->use activateWindow
    23. //and other desired behaviours
    24. }
    To copy to clipboard, switch view to plain text mode 

    Shame on me I didn't saw the sentence about activationg the application window,I tougth the autor wants only prevent the other instances from running :/

Similar Threads

  1. How to find Application Data directory?
    By Teuniz in forum Qt Programming
    Replies: 7
    Last Post: 26th July 2013, 15:02
  2. Replies: 3
    Last Post: 20th October 2010, 23:36
  3. window on top of an application
    By siddhu in forum Qt Programming
    Replies: 9
    Last Post: 16th December 2009, 15:53
  4. Replies: 2
    Last Post: 10th December 2009, 08:01
  5. Multi Window Application in QT/C++
    By pshah.mumbai in forum Newbie
    Replies: 8
    Last Post: 8th July 2008, 18:21

Tags for this Thread

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.