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