Added an example of an application sending it's arguments to the primary instance

This commit is contained in:
Itay Grudev 2016-08-10 02:43:52 +01:00
parent 6f585973dc
commit 596cf23bae
No known key found for this signature in database
GPG Key ID: 913C021BA6F9DE98
4 changed files with 61 additions and 0 deletions

View File

@ -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();
}

View File

@ -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;
}

View File

@ -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

View File

@ -0,0 +1,9 @@
# Single Application implementation
include(../../singleapplication.pri)
DEFINES += QAPPLICATION_CLASS=QCoreApplication
SOURCES += main.cpp \
messagereceiver.cpp
HEADERS += \
messagereceiver.h