本文主要是介绍三 、官方示例 directconnectclient(simpleSwitch),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- Source 代码
- 1、创建源对象
- 2 创建registry
- 3 创建主机节点
- 4 共享主机节点到QtRo
- Replica 代码
- 1 使用repc将副本添加到项目中
- 2 创建一个节点以连接源的主机节点
- 3 调用节点的acquire()来创建指向复制副本的指针
本示例采用了直接连接,并用了静态的Rep文件
Source 代码
1、创建源对象
To create this Source object, first we create the definition file, simpleswitch.rep. This file describes the properties and methods for the object and is input to the Qt Remote Objects Compiler repc. This file only defines interfaces that are necessary to expose to the Replicas.
要创建这个源对象,首先创建定义文件simpleswitch.rep。该文件描述对象的属性和方法,并输入到Qt repc编译器。此文件仅定义Replicas副本所需的接口。
simpleswitch.rep
class SimpleSwitch{PROP(bool currState=false);SLOT(server_slot(bool clientState));};
In simpleswitch.rep,
currState
保存当前switch的开关状态.
server_slot()
链接到 echoSwitchState(bool newstate) 信号。
只有在Pro文件中,添加下面这句话,rep文件才会被Qt的repc编译器处理:
REPC_SOURCE = simpleswitch.rep
REPC_SOURCE 包含在 Qt Remote Objects模块中:
QT += remoteobjects
repc创建rep_SimpleSwitch_source.h头文件。repc创建了三个用于QtRO的相关类。对于本例,我们使用基本的:SimpleSwitchSimpleSource。它是一个抽象类,在rep_SimpleSwitch_source.h中定义, 我们从中派生出SimpleSwitch实现类:
simpleswitch.h
#ifndef SIMPLESWITCH_H#define SIMPLESWITCH_H#include "rep_SimpleSwitch_source.h"class SimpleSwitch : public SimpleSwitchSimpleSource{Q_OBJECTpublic:SimpleSwitch(QObject *parent = nullptr);~SimpleSwitch();virtual void server_slot(bool clientState);public Q_SLOTS:void timeout_slot();private:QTimer *stateChangeTimer;};#endif
在simpleswitch.h中
stateChangeTimer是一个用于切换SimpleSwitch状态的QTimer。
timeout_slot()连接到stateChangeTimer的timeout()信号。
server_slot()——每当任何副本调用时,都会在源上自动调用它
currStateChanged(bool),在repc生成的rep_SimpleSwitch.h, 每当currState切换时都会发出。在本例中,我们忽略源端的信号,然后在副本端处理它。
simpleswitch.cpp
#include "simpleswitch.h"// constructorSimpleSwitch::SimpleSwitch(QObject *parent) : SimpleSwitchSimpleSource(parent){stateChangeTimer = new QTimer(this); // Initialize timerQObject::connect(stateChangeTimer, SIGNAL(timeout()), this, SLOT(timeout_slot())); // connect timeout() signal from stateChangeTimer to timeout_slot() of simpleSwitchstateChangeTimer->start(2000); // Start timer and set timout to 2 secondsqDebug() << "Source Node Started";}//destructorSimpleSwitch::~SimpleSwitch(){stateChangeTimer->stop();}void SimpleSwitch::server_slot(bool clientState){qDebug() << "Replica state is " << clientState; // print switch state echoed back by client}void SimpleSwitch::timeout_slot(){// slot called on timer timeoutif (currState()) // check if current state is true, currState() is defined in repc generated rep_SimpleSwitch_source.hsetCurrState(false); // set state to falseelsesetCurrState(true); // set state to trueqDebug() << "Source State is "<<currState();}
2 创建registry
本例中用到的是直接连接,所以省略此步骤
3 创建主机节点
主机节点的创建过程如下:
QRemoteObjectHost srcNode(QUrl(QStringLiteral("local:switch")));
4 共享主机节点到QtRo
共享主机节点到QtRo网络:
SimpleSwitch srcSwitch; // create simple switchsrcNode.enableRemoting(&srcSwitch); // enable remoting
main.cpp
#include <QCoreApplication>#include "simpleswitch.h"int main(int argc, char *argv[]){QCoreApplication a(argc, argv);SimpleSwitch srcSwitch; // create simple switchQRemoteObjectHost srcNode(QUrl(QStringLiteral("local:switch"))); // create host node without RegistrysrcNode.enableRemoting(&srcSwitch); // enable remoting/sharingreturn a.exec();}
编译并运行这个源代码端项目。在不创建任何复制副本的情况下,输出应如下所示,开关状态每两秒在true和false之间切换

接下来的步骤是创建网络的副本端,在本例中,副本端从源获取switch状态并将其返回给主机节点。
Replica 代码
1 使用repc将副本添加到项目中
我们使用与源端相同的rep文件,SimpleSwitch.rep
REPC_REPLICA = simpleswitch.rep
2 创建一个节点以连接源的主机节点
实例化网络上的第二个节点,并将其与源主机节点连接:
QRemoteObjectNode repNode; // create remote object noderepNode.connectToNode(QUrl(QStringLiteral("local:switch"))); // connect with remote host node
3 调用节点的acquire()来创建指向复制副本的指针
首先,实例化一个 replica:
QSharedPointer<SimpleSwitchReplica> ptr;ptr.reset(repNode.acquire<SimpleSwitchReplica>()); // acquire replica of source from host node
注意:acquire()返回指向复制副本的指针,我们用QSharedPointer或QScopedPointer指向他,以确保始终能够正确删除。
main.cpp
#include <QCoreApplication>#include "client.h"int main(int argc, char *argv[]){QCoreApplication a(argc, argv);QSharedPointer<SimpleSwitchReplica> ptr; // shared pointer to hold source replicaQRemoteObjectNode repNode; // create remote object noderepNode.connectToNode(QUrl(QStringLiteral("local:switch"))); // connect with remote host nodeptr.reset(repNode.acquire<SimpleSwitchReplica>()); // acquire replica of source from host nodeClient rswitch(ptr); // create client switch object and pass reference of replica to itreturn a.exec();}
client.h
#ifndef _CLIENT_H#define _CLIENT_H#include <QObject>#include <QSharedPointer>#include "rep_SimpleSwitch_replica.h"class Client : public QObject{Q_OBJECTpublic:Client(QSharedPointer<SimpleSwitchReplica> ptr);~Client();void initConnections();// Function to connect signals and slots of source and clientQ_SIGNALS:void echoSwitchState(bool switchState);// this signal is connected with server_slot(..) on the source object and echoes back switch state received from sourcepublic Q_SLOTS:void recSwitchState_slot(); // slot to receive source stateprivate:bool clientSwitchState; // holds received server switch stateQSharedPointer<SimpleSwitchReplica> reptr;// holds reference to replica};#endif
client.cpp
#include "client.h"// constructorClient::Client(QSharedPointer<SimpleSwitchReplica> ptr) :QObject(nullptr),reptr(ptr){initConnections();//We can connect to SimpleSwitchReplica Signals/Slots//directly because our Replica was generated by repc.}//destructorClient::~Client(){}void Client::initConnections(){// initialize connections between signals and slots// connect source replica signal currStateChanged() with client's recSwitchState() slot to receive source's current stateQObject::connect(reptr.data(), SIGNAL(currStateChanged()), this, SLOT(recSwitchState_slot()));// connect client's echoSwitchState(..) signal with replica's server_slot(..) to echo back received stateQObject::connect(this, SIGNAL(echoSwitchState(bool)),reptr.data(), SLOT(server_slot(bool)));}void Client::recSwitchState_slot(){qDebug() << "Received source state "<<reptr.data()->currState();clientSwitchState = reptr.data()->currState();Q_EMIT echoSwitchState(clientSwitchState); // Emit signal to echo received state back to server}
将此示例与源代码端示例一起编译并运行会生成以下输出:

这篇关于三 、官方示例 directconnectclient(simpleSwitch)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!