mirror of
https://github.com/itay-grudev/SingleApplication.git
synced 2024-11-15 12:15:43 +08:00
Added an example of an application sending it's arguments to the primary instance
This commit is contained in:
parent
6f585973dc
commit
596cf23bae
25
examples/sending_arguments/main.cpp
Executable file
25
examples/sending_arguments/main.cpp
Executable 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();
|
||||||
|
}
|
12
examples/sending_arguments/messagereceiver.cpp
Normal file
12
examples/sending_arguments/messagereceiver.cpp
Normal 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;
|
||||||
|
}
|
15
examples/sending_arguments/messagereceiver.h
Normal file
15
examples/sending_arguments/messagereceiver.h
Normal 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
|
9
examples/sending_arguments/sending_arguments.pro
Executable file
9
examples/sending_arguments/sending_arguments.pro
Executable file
@ -0,0 +1,9 @@
|
|||||||
|
# Single Application implementation
|
||||||
|
include(../../singleapplication.pri)
|
||||||
|
DEFINES += QAPPLICATION_CLASS=QCoreApplication
|
||||||
|
|
||||||
|
SOURCES += main.cpp \
|
||||||
|
messagereceiver.cpp
|
||||||
|
|
||||||
|
HEADERS += \
|
||||||
|
messagereceiver.h
|
Loading…
Reference in New Issue
Block a user