You need to store your replica in a class instance that can be accessed from QML. You can connect replica's signal to signal that you declared in the class then receive it from QML.
Please review the code below. (replicaexample.h, replicaexample.cpp and main.qml)
(Host Application is a console app and client is a QtQuick app.)
Host Application
"Hello! I am sending message, counter: 0"
"Hello! I am sending message, counter: 1"
"Hello! I am sending message, counter: 2"
"Hello! I am sending message, counter: 3"
"Hello! I am sending message, counter: 4"
"Hello! I am sending message, counter: 5"
ExampleReplica.rep
class ExampleRep
{
SIGNAL(my_signal(QString message));
};
sourceexample.h
#ifndef SOURCEEXAMPLE_H
#define SOURCEEXAMPLE_H
#include <QTimer>
#include <QDebug>
#include "rep_ExampleReplica_source.h"
class SourceExample : public ExampleRepSimpleSource
{
Q_OBJECT
public:
SourceExample(QObject* parent = nullptr);
~SourceExample();
private Q_SLOTS:
void _timerSlot();
private:
int _counter;
QTimer* _timer;
QString _message;
};
#endif // SOURCEEXAMPLE_H
sourceexample.cpp
#include "sourceexample.h"
SourceExample::SourceExample(QObject* parent)
: ExampleRepSimpleSource(parent)
, _counter(0)
{
_timer = new QTimer(this);
_timer->setInterval(2000);
connect(_timer, &QTimer::timeout, this, &SourceExample::_timerSlot);
_timer->start();
}
SourceExample::~SourceExample()
{
}
void SourceExample::_timerSlot()
{
_message = "Hello! I am sending message, counter: " + QString::number(_counter++);
qInfo() << _message;
Q_EMIT my_signal(_message);
}
main.cpp
#include <QCoreApplication>
#include <iostream>
#include "sourceexample.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
SourceExample sourceObject;
QRemoteObjectHost srcNode(QUrl(QStringLiteral("local:sourceNode")));
srcNode.enableRemoting(&sourceObject);
return a.exec();
}
Client Application
qml: console log: Hello! I am sending message, counter: 0
qml: console log: Hello! I am sending message, counter: 1
qml: console log: Hello! I am sending message, counter: 2
qml: console log: Hello! I am sending message, counter: 3
qml: console log: Hello! I am sending message, counter: 4
qml: console log: Hello! I am sending message, counter: 5
replicaexample.h
#ifndef REPLICAEXAMPLE_H
#define REPLICAEXAMPLE_H
#include <QObject>
#include <QSharedPointer>
#include <QDebug>
#include "rep_ExampleReplica_replica.h"
class ReplicaExample : public QObject
{
Q_OBJECT
public:
ReplicaExample(QSharedPointer<ExampleRepReplica> repNode, QObject* parent = nullptr);
~ReplicaExample();
Q_SIGNALS:
void my_signal(QString message);
private:
QSharedPointer<ExampleRepReplica> _repNode;
};
#endif // REPLICAEXAMPLE_H
replicaexample.cpp
#include "replicaexample.h"
ReplicaExample::ReplicaExample(QSharedPointer<ExampleRepReplica> repNode, QObject* parent)
: QObject(parent)
, _repNode(repNode)
{
QObject::connect(_repNode.data(), &ExampleRepReplica::my_signal, this, &ReplicaExample::my_signal);
}
ReplicaExample::~ReplicaExample()
{
}
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlEngine>
#include <QQmlContext>
#include "replicaexample.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QSharedPointer<ExampleRepReplica> ptr;
QRemoteObjectNode repNode;
repNode.connectToNode(QUrl(QStringLiteral("local:sourceNode")));
ptr.reset(repNode.acquire<ExampleRepReplica>());
ReplicaExample client(ptr);
QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/main.qml"));
engine.rootContext()->setContextProperty("repObject", &client);
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
Connections {
target: repObject
function onMy_signal(message) {
console.log("console log: " + message)
}
}
}