Results 1 to 9 of 9

Thread: Single Application Instance

  1. #1
    Join Date
    Mar 2009
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Single Application Instance

    I want to share this simple solution if it is a good idea, or get some response if it is a bad idea. It is a way to check if application is allready running. Also i want to ask if someone has at good ide on how to raise the running application when trying to start the second.

    Header:
    Qt Code:
    1. #ifndef APPLICATION_HH
    2. #define APPLICATION_HH 1
    3.  
    4. #include <QtGui/QApplication>
    5.  
    6. class QSharedMemory;
    7.  
    8. class Application:public QApplication{
    9.  
    10. Q_OBJECT
    11.  
    12. public:
    13. Application(int &argc, char **argv);
    14. ~Application();
    15.  
    16. bool lock();
    17.  
    18. private:
    19. QSharedMemory *_singular;
    20. };
    21.  
    22. #endif //APPLICATION_HH
    To copy to clipboard, switch view to plain text mode 
    Source:
    Qt Code:
    1. #include <QtCore/QSharedMemory>
    2.  
    3. #include "Application.hh"
    4.  
    5. Application::Application(int &argc, char **argv):QApplication(argc, argv, true)
    6. {
    7. _singular = new QSharedMemory("MyVeryUniqueName", this);
    8. }
    9.  
    10. Application::~Application()
    11. {
    12. if(_singular->isAttached())
    13. _singular->detach();
    14. }
    15.  
    16. bool Application::lock()
    17. {
    18. if(_singular->attach(QSharedMemory::ReadOnly)){
    19. _singular->detach();
    20. return false;
    21. }
    22.  
    23. if(_singular->create(1))
    24. return true;
    25.  
    26. return false;
    27. }
    To copy to clipboard, switch view to plain text mode 
    Usage:
    Qt Code:
    1. Application app(argc, argv);
    2. if(!app.lock()){
    3. QMessageBox::critical(0, "Error", "Application allready running");
    4. exit(1);
    5. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Single Application Instance

    You may want have a look here: SingleApplication
    With the above mentioned you can send a message and react on it, e.g. show and process something...
    Last edited by Lykurg; 20th March 2009 at 12:23.

  3. #3
    Join Date
    Jun 2008
    Posts
    35
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Single Application Instance

    you can create advisory-lock file and check its status in main().

  4. #4
    Join Date
    Mar 2009
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Single Application Instance

    Allright! Thanks.

    Exactly what is was looking for

  5. #5
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Single Application Instance

    I thought you can only have 1 instance of QApplication? That instance is stored in qApp as global vairable? Do I misunderstand you?

    So, do

    if ( qApp != NULL ) {
    // application has been created
    } else {
    // create an application
    }

  6. #6
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Single Application Instance

    Quote Originally Posted by lni View Post
    I thought you can only have 1 instance of QApplication? That instance is stored in qApp as global vairable? Do I misunderstand you?

    So, do

    if ( qApp != NULL ) {
    // application has been created
    } else {
    // create an application
    }
    After reading the page at http://doc.trolltech.com/solutions/4...ion/index.html, I understand what it is. But why I don't have QtSingleApplication in my package?

    I think it is very useful class. Is it possible to detect a running application in different machine? I am currently having several processes across different machines writing to same files, and I have to create/delete a file.lock to prevent the processes writing to the same file at the same time...

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Single Application Instance

    Quote Originally Posted by lni View Post
    But why I don't have QtSingleApplication in my package?
    It's only for commercial use: http://www.qtsoftware.com/products/a...leapplication/

  8. #8
    Join Date
    Nov 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Single Application Instance

    Quote Originally Posted by Lykurg View Post
    You may want have a look here: SingleApplication
    With the above mentioned you can send a message and react on it, e.g. show and process something...
    Just so everybody who uses this implementation using SingleApplication and more specifically the second one with QSharedMemory and QTimer...you have to modify the original code.
    originally in the constructor it says
    Qt Code:
    1. if (!sharedMemory.create(byteArray.size()))
    To copy to clipboard, switch view to plain text mode 
    you must change byteArray.size() to a number of bytes big enough to hold your message. The way it is right now, its only like 1 byte big. I had the darndest time finding out what was wrong.
    Too bad it doesn't dynamically change in size. I set it to 30000, which is excessive, but I'm passing filenames and that can take up a lot of space.

    But now, it works great. Thanks.

  9. #9
    Join Date
    Nov 2014
    Posts
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Single Application Instance

    Quote Originally Posted by Lykurg View Post
    You may want have a look here: SingleApplication
    With the above mentioned you can send a message and react on it, e.g. show and process something...

    Good day

    I'm designing an application based on Qt 5 and one of the requirements is to have it allow only one application instance running.
    I have used the code found at the link you provided and it works perfect. So I want to include it in my application.

    My question is: does this code has a copyright and/or license information attached to it?

    Thanks.
    Last edited by adrian.scarlat; 4th November 2014 at 15:12.

Similar Threads

  1. Replies: 5
    Last Post: 9th June 2008, 11:21
  2. Only let one instance of the application run
    By chrisdsimpson in forum Newbie
    Replies: 1
    Last Post: 9th April 2008, 21:30
  3. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  4. Best Practice on single source Qt Desktop/Qtopia Core/QPE application
    By izico in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 31st March 2007, 07:08
  5. Replies: 5
    Last Post: 16th January 2006, 05:15

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.