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:
#ifndef APPLICATION_HH
#define APPLICATION_HH 1
#include <QtGui/QApplication>
class QSharedMemory;
Q_OBJECT
public:
Application(int &argc, char **argv);
~Application();
bool lock();
private:
QSharedMemory *_singular;
};
#endif //APPLICATION_HH
#ifndef APPLICATION_HH
#define APPLICATION_HH 1
#include <QtGui/QApplication>
class QSharedMemory;
class Application:public QApplication{
Q_OBJECT
public:
Application(int &argc, char **argv);
~Application();
bool lock();
private:
QSharedMemory *_singular;
};
#endif //APPLICATION_HH
To copy to clipboard, switch view to plain text mode
Source:
#include <QtCore/QSharedMemory>
#include "Application.hh"
Application
::Application(int &argc,
char **argv
):QApplication(argc, argv,
true){
_singular = new QSharedMemory("MyVeryUniqueName", this);
}
Application::~Application()
{
if(_singular->isAttached())
_singular->detach();
}
bool Application::lock()
{
if(_singular->attach(QSharedMemory::ReadOnly)){
_singular->detach();
return false;
}
if(_singular->create(1))
return true;
return false;
}
#include <QtCore/QSharedMemory>
#include "Application.hh"
Application::Application(int &argc, char **argv):QApplication(argc, argv, true)
{
_singular = new QSharedMemory("MyVeryUniqueName", this);
}
Application::~Application()
{
if(_singular->isAttached())
_singular->detach();
}
bool Application::lock()
{
if(_singular->attach(QSharedMemory::ReadOnly)){
_singular->detach();
return false;
}
if(_singular->create(1))
return true;
return false;
}
To copy to clipboard, switch view to plain text mode
Usage:
Application app(argc, argv);
if(!app.lock()){
QMessageBox::critical(0,
"Error",
"Application allready running");
exit(1);
}
Application app(argc, argv);
if(!app.lock()){
QMessageBox::critical(0, "Error", "Application allready running");
exit(1);
}
To copy to clipboard, switch view to plain text mode
Bookmarks