您需要将副本存储在可以从 QML 访问的类实例中。您可以将副本的信号连接到您在类中声明的信号,然后从 QML 接收它。
请查看下面的代码。(replicaexample.h、replicaexample.cpp 和 main.qml)
(主机应用程序是一个控制台应用程序,客户端是一个 QtQuick 应用程序。)
主机应用程序
"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));
};
源示例.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
源示例.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);
}
主文件
#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();
}
客户端应用程序
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
副本示例.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
副本示例.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()
{
}
主文件
#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)
}
}
}