From 596cf23bae223d043daf57dcb7b118e149d488e6 Mon Sep 17 00:00:00 2001 From: Itay Grudev Date: Wed, 10 Aug 2016 02:43:52 +0100 Subject: [PATCH] Added an example of an application sending it's arguments to the primary instance --- examples/sending_arguments/main.cpp | 25 +++++++++++++++++++ .../sending_arguments/messagereceiver.cpp | 12 +++++++++ examples/sending_arguments/messagereceiver.h | 15 +++++++++++ .../sending_arguments/sending_arguments.pro | 9 +++++++ 4 files changed, 61 insertions(+) create mode 100755 examples/sending_arguments/main.cpp create mode 100644 examples/sending_arguments/messagereceiver.cpp create mode 100644 examples/sending_arguments/messagereceiver.h create mode 100755 examples/sending_arguments/sending_arguments.pro diff --git a/examples/sending_arguments/main.cpp b/examples/sending_arguments/main.cpp new file mode 100755 index 0000000..8368036 --- /dev/null +++ b/examples/sending_arguments/main.cpp @@ -0,0 +1,25 @@ +#include +#include "messagereceiver.h" + +int main(int argc, char *argv[]) +{ + // Allow secondary instances + SingleApplication app( argc, argv, true ); + + MessageReceiver msgReceiver; + + // If this is a secondary instance + if( app.isSecondary() ) { + app.sendMessage( app.arguments().join(' ').toUtf8() ); + return 0; + } else { + QObject::connect( + &app, + &SingleApplication::receivedMessage, + &msgReceiver, + &MessageReceiver::receivedMessage + ); + } + + return app.exec(); +} diff --git a/examples/sending_arguments/messagereceiver.cpp b/examples/sending_arguments/messagereceiver.cpp new file mode 100644 index 0000000..0560b07 --- /dev/null +++ b/examples/sending_arguments/messagereceiver.cpp @@ -0,0 +1,12 @@ +#include +#include "messagereceiver.h" + +MessageReceiver::MessageReceiver(QObject *parent) : QObject(parent) +{ +} + +void MessageReceiver::receivedMessage(int instanceId, QByteArray message) +{ + qDebug() << "Received message from instance: " << instanceId; + qDebug() << "Message Text: " << message; +} diff --git a/examples/sending_arguments/messagereceiver.h b/examples/sending_arguments/messagereceiver.h new file mode 100644 index 0000000..50a970c --- /dev/null +++ b/examples/sending_arguments/messagereceiver.h @@ -0,0 +1,15 @@ +#ifndef MESSAGERECEIVER_H +#define MESSAGERECEIVER_H + +#include + +class MessageReceiver : public QObject +{ + Q_OBJECT +public: + explicit MessageReceiver(QObject *parent = 0); +public slots: + void receivedMessage( int instanceId, QByteArray message ); +}; + +#endif // MESSAGERECEIVER_H diff --git a/examples/sending_arguments/sending_arguments.pro b/examples/sending_arguments/sending_arguments.pro new file mode 100755 index 0000000..897636a --- /dev/null +++ b/examples/sending_arguments/sending_arguments.pro @@ -0,0 +1,9 @@ +# Single Application implementation +include(../../singleapplication.pri) +DEFINES += QAPPLICATION_CLASS=QCoreApplication + +SOURCES += main.cpp \ + messagereceiver.cpp + +HEADERS += \ + messagereceiver.h