如何使用QT远程对象将信号从源发送到副本



如何从源c++文件发出信号,并使用副本文件中的连接捕获信号。我已经在rep文件中声明了信号和发出信号的插槽。正在尝试使用Connections:和target作为复制对象捕获信号。我已经在客户端qt引擎中设置了replicaobject的上下文。//.rep文件等级测试{SLOT(test(((;SIGNAL(testsignal(((;}

//Interface.cpp file in source side
void Interface::test()
{
Q_EMIT testsignal();
}

//客户端中的main.qml连接{目标:Intercereplicaon测试信号:{console.log("警报!!"(;}

您需要将副本存储在可以从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

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

客户端应用程序

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

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)
}
}
}

最新更新