QApplication inherit macro

This commit is contained in:
Itay Grudev 2015-06-09 17:29:20 +03:00
parent 2dd9a4e553
commit 219383657a
3 changed files with 27 additions and 4 deletions

View File

@ -32,6 +32,24 @@ The ```Show Up``` signal
------------------------ ------------------------
The SingleApplication class implements a ```showUp()``` signal. You can bind to that signal to raise your application's window when a new instance had been started. The SingleApplication class implements a ```showUp()``` signal. You can bind to that signal to raise your application's window when a new instance had been started.
Note that since ```SingleApplication``` extends the ```QApplication``` class you can do the following:
```cpp
QObject::connect(QApplication::instance(), SIGNAL(showUp()), window, SLOT(raise())); // window is your QWindow instance
```
Using ```QApplication::instance()``` is a neat way to get the ```SingleApplication``` instance at any place in your program.
Extending from other application classes
-----------------------
```SingleApplication``` extends from the ```QApplication``` class by default , but you can easily change that to ```QGuiApplication``` or ```QCoreApplication``` from the ```singleapplication.h``` file, using the ```QAPPLICATION_CLASS``` macro.
__Example:__
```cpp
#define QAPPLICATION_CLASS QCoreApplication
```
Implementation Implementation
-------------- --------------
The library is implemented with a QSharedMemory block which is thread safe and guarantees a race condition will not occur. It also uses a QLocalSocket to notify the main process that a new instance had been spawned and thus invoke the ```showUp()``` signal. The library is implemented with a QSharedMemory block which is thread safe and guarantees a race condition will not occur. It also uses a QLocalSocket to notify the main process that a new instance had been spawned and thus invoke the ```showUp()``` signal.

View File

@ -75,9 +75,9 @@ public:
* @param argv * @param argv
*/ */
SingleApplication::SingleApplication(int &argc, char *argv[]) SingleApplication::SingleApplication(int &argc, char *argv[])
: QApplication(argc, argv), d_ptr(new SingleApplicationPrivate(this)) : QAPPLICATION_CLASS(argc, argv), d_ptr(new SingleApplicationPrivate(this))
{ {
QString serverName = QApplication::organizationName() + QApplication::applicationName(); QString serverName = QAPPLICATION_CLASS::organizationName() + QAPPLICATION_CLASS::applicationName();
serverName.replace(QRegExp("[^\\w\\-. ]"), ""); serverName.replace(QRegExp("[^\\w\\-. ]"), "");
// Garantee thread safe behaviour with a shared memory block // Garantee thread safe behaviour with a shared memory block

View File

@ -1,7 +1,12 @@
#ifndef SINGLE_APPLICATION_H #ifndef SINGLE_APPLICATION_H
#define SINGLE_APPLICATION_H #define SINGLE_APPLICATION_H
#include <QApplication> // Change this to inherit from QGuiApplication or QCoreApplication
#define QAPPLICATION_CLASS QApplication
#define QUOTE(C) #C
#define INCLUDE_FILE(C) QUOTE(C)
#include INCLUDE_FILE(QAPPLICATION_CLASS)
class SingleApplicationPrivate; class SingleApplicationPrivate;
@ -9,7 +14,7 @@ class SingleApplicationPrivate;
* @brief The SingleApplication class handles multipe instances of the same Application * @brief The SingleApplication class handles multipe instances of the same Application
* @see QApplication * @see QApplication
*/ */
class SingleApplication : public QApplication class SingleApplication : public QAPPLICATION_CLASS
{ {
Q_OBJECT Q_OBJECT
public: public: