Skip to content

Commit

Permalink
Added an example of an application sending it's arguments to the prim…
Browse files Browse the repository at this point in the history
…ary instance
  • Loading branch information
itay-grudev committed Aug 10, 2016
1 parent 6f58597 commit 596cf23
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
25 changes: 25 additions & 0 deletions examples/sending_arguments/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <SingleApplication.h>
#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();
}
12 changes: 12 additions & 0 deletions examples/sending_arguments/messagereceiver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <QDebug>
#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;
}
15 changes: 15 additions & 0 deletions examples/sending_arguments/messagereceiver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef MESSAGERECEIVER_H
#define MESSAGERECEIVER_H

#include <QObject>

class MessageReceiver : public QObject
{
Q_OBJECT
public:
explicit MessageReceiver(QObject *parent = 0);
public slots:
void receivedMessage( int instanceId, QByteArray message );
};

#endif // MESSAGERECEIVER_H
9 changes: 9 additions & 0 deletions examples/sending_arguments/sending_arguments.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Single Application implementation
include(../../singleapplication.pri)
DEFINES += QAPPLICATION_CLASS=QCoreApplication

SOURCES += main.cpp \
messagereceiver.cpp

HEADERS += \
messagereceiver.h

0 comments on commit 596cf23

Please sign in to comment.