Allow running only one instance of application.
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
#include "application.h"
|
||||
|
||||
Application::Application(int &argc, char **argv)
|
||||
:QApplication(argc, argv)
|
||||
{
|
||||
m_single = new QSharedMemory("ShredMemoryForOneInstanceOfProdejnaApp", this);
|
||||
}
|
||||
|
||||
Application::~Application()
|
||||
{
|
||||
if (m_single->isAttached())
|
||||
{
|
||||
m_single->detach();
|
||||
}
|
||||
}
|
||||
|
||||
bool Application::lock()
|
||||
{
|
||||
if (m_single->attach(QSharedMemory::ReadOnly))
|
||||
{
|
||||
m_single->detach();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_single->create(1))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
#ifndef APPLICATION_H
|
||||
#define APPLICATION_H
|
||||
|
||||
#include <QApplication>
|
||||
#include <QSharedMemory>
|
||||
|
||||
class Application : public QApplication
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Application(int &argc, char **argv);
|
||||
~Application();
|
||||
|
||||
bool lock();
|
||||
|
||||
private:
|
||||
QSharedMemory *m_single;
|
||||
};
|
||||
|
||||
#endif // APPLICATION_H
|
||||
@@ -28,10 +28,12 @@ win32 {
|
||||
|
||||
SOURCES += main.cpp\
|
||||
mainwindow.cpp \
|
||||
logindialog.cpp
|
||||
logindialog.cpp \
|
||||
application.cpp
|
||||
|
||||
HEADERS += mainwindow.h \
|
||||
logindialog.h
|
||||
logindialog.h \
|
||||
application.h
|
||||
|
||||
FORMS += mainwindow.ui \
|
||||
logindialog.ui
|
||||
|
||||
+10
-1
@@ -3,6 +3,9 @@
|
||||
#include <QDesktopWidget>
|
||||
#include <QTranslator>
|
||||
#include <QLibraryInfo>
|
||||
#include <QMessageBox>
|
||||
|
||||
#include "application.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <Windows.h>
|
||||
@@ -10,7 +13,13 @@
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
Application a(argc, argv);
|
||||
|
||||
if (!a.lock())
|
||||
{
|
||||
QMessageBox::warning(NULL, "Prodejna is running", "Prodejna is allready running. Only one instance can be started.");
|
||||
return -42;
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
QString exePath = a.applicationDirPath();
|
||||
|
||||
Reference in New Issue
Block a user