diff --git a/application/application.cpp b/application/application.cpp new file mode 100644 index 0000000..833904b --- /dev/null +++ b/application/application.cpp @@ -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; +} diff --git a/application/application.h b/application/application.h new file mode 100644 index 0000000..259e5b0 --- /dev/null +++ b/application/application.h @@ -0,0 +1,21 @@ +#ifndef APPLICATION_H +#define APPLICATION_H + +#include +#include + +class Application : public QApplication +{ + Q_OBJECT + +public: + Application(int &argc, char **argv); + ~Application(); + + bool lock(); + +private: + QSharedMemory *m_single; +}; + +#endif // APPLICATION_H diff --git a/application/application.pro b/application/application.pro index 9f28ab0..d7edcc3 100644 --- a/application/application.pro +++ b/application/application.pro @@ -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 diff --git a/application/main.cpp b/application/main.cpp index d15d3a0..4fda4ee 100644 --- a/application/main.cpp +++ b/application/main.cpp @@ -3,6 +3,9 @@ #include #include #include +#include + +#include "application.h" #ifdef _WIN32 #include @@ -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();