三 、官方示例 directconnectclient(simpleSwitch)

2024-06-20 07:38

本文主要是介绍三 、官方示例 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)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1077510

相关文章

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

C++11作用域枚举(Scoped Enums)的实现示例

《C++11作用域枚举(ScopedEnums)的实现示例》枚举类型是一种非常实用的工具,C++11标准引入了作用域枚举,也称为强类型枚举,本文主要介绍了C++11作用域枚举(ScopedEnums... 目录一、引言二、传统枚举类型的局限性2.1 命名空间污染2.2 整型提升问题2.3 类型转换问题三、C

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

C++ 检测文件大小和文件传输的方法示例详解

《C++检测文件大小和文件传输的方法示例详解》文章介绍了在C/C++中获取文件大小的三种方法,推荐使用stat()函数,并详细说明了如何设计一次性发送压缩包的结构体及传输流程,包含CRC校验和自动解... 目录检测文件的大小✅ 方法一:使用 stat() 函数(推荐)✅ 用法示例:✅ 方法二:使用 fsee

mysql查询使用_rowid虚拟列的示例

《mysql查询使用_rowid虚拟列的示例》MySQL中,_rowid是InnoDB虚拟列,用于无主键表的行ID查询,若存在主键或唯一列,则指向其,否则使用隐藏ID(不稳定),推荐使用ROW_NUM... 目录1. 基本查询(适用于没有主键的表)2. 检查表是否支持 _rowid3. 注意事项4. 最佳实

HTML中meta标签的常见使用案例(示例详解)

《HTML中meta标签的常见使用案例(示例详解)》HTMLmeta标签用于提供文档元数据,涵盖字符编码、SEO优化、社交媒体集成、移动设备适配、浏览器控制及安全隐私设置,优化页面显示与搜索引擎索引... 目录html中meta标签的常见使用案例一、基础功能二、搜索引擎优化(seo)三、社交媒体集成四、移动

HTML input 标签示例详解

《HTMLinput标签示例详解》input标签主要用于接收用户的输入,随type属性值的不同,变换其具体功能,本文通过实例图文并茂的形式给大家介绍HTMLinput标签,感兴趣的朋友一... 目录通用属性输入框单行文本输入框 text密码输入框 password数字输入框 number电子邮件输入编程框