三 、官方示例 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

相关文章

53、Flink Interval Join 代码示例

1、概述 interval Join 默认会根据 keyBy 的条件进行 Join 此时为 Inner Join; interval Join 算子的水位线会取两条流中水位线的最小值; interval Join 迟到数据的判定是以 interval Join 算子的水位线为基准; interval Join 可以分别输出两条流中迟到的数据-[sideOutputLeftLateData,

【Unity Shader】Alpha Blend(Alpha混合)的概念及其使用示例

在Unity和图形编程中,Alpha Blend(也称为Alpha混合)是一种用于处理像素透明度的技术。它允许像素与背景像素融合,从而实现透明或半透明的效果。Alpha Blend在渲染具有透明度的物体(如窗户、玻璃、水、雾等)时非常重要。 Alpha Blend的概念: Alpha值:Alpha值是一个介于0(完全透明)和1(完全不透明)的数值,用于表示像素的透明度。混合模式:Alpha B

OSG学习:阴影代码示例

效果图: 代码示例: #include <osgViewer/Viewer>#include <osg/Node>#include <osg/Geode>#include <osg/Group>#include <osg/Camera>#include <osg/ShapeDrawable>#include <osg/ComputeBoundsVisitor>#include

OSG学习:转动的小汽车示例

由于只是简单的示例,所以小汽车的模型也比较简单,是由简单的几何体组成。 代码如下: #include <osg\ShapeDrawable>#include <osg\AnimationPath>#include <osg\MatrixTransform>#include<osgDB\ReadFile>#include<osgViewer\Viewer>osg::MatrixTr

OSG学习:使用已有回调示例

回调的类型有很多种,一般很容易就想到的是UpdateCallBack,或者EventCallBack,回调的意思就是说,你可以规定在某件事情发生时启动一个函数,这个函数可能做一些事情。这个函数就叫做回调函数。 #include<osg\MatrixTransform>#include<osg\PositionAttitudeTransform>#include<osg\Geode>#

线性回归(Linear Regression)原理详解及Python代码示例

一、线性回归原理详解         线性回归是一种基本的统计方法,用于预测因变量(目标变量)与一个或多个自变量(特征变量)之间的线性关系。线性回归模型通过拟合一条直线(在多变量情况下是一条超平面)来最小化预测值与真实值之间的误差。 1. 线性回归模型         对于单变量线性回归,模型的表达式为:         其中: y是目标变量。x是特征变量。β0是截距项(偏置)。β1

LoRaWAN在嵌入式网络通信中的应用:打造高效远程监控系统(附代码示例)

引言 随着物联网(IoT)技术的发展,远程监控系统在各个领域的应用越来越广泛。LoRaWAN(Long Range Wide Area Network)作为一种低功耗广域网通信协议,因其长距离传输、低功耗和高可靠性等特点,成为实现远程监控的理想选择。本文将详细介绍LoRaWAN的基本原理、应用场景,并通过一个具体的项目展示如何使用LoRaWAN实现远程监控系统。希望通过图文并茂的讲解,帮助读

一二三应用开发平台应用开发示例(4)——视图类型介绍以及新增、修改、查看视图配置

调整上级属性类型 前面为了快速展示平台的低代码配置功能,将实体文件夹的数据模型上级属性的数据类型暂时配置为文本类型,现在我们调整下,将其数据类型调整为实体,如下图所示: 数据类型需要选择实体,并在实体选择框中选择自身“文件夹” 这时候,再点击生成代码,平台会报错,提示“实体【文件夹】未设置主参照视图”。这是因为文件夹选择的功能页面,同样是基于配置产生的,因为视图我们还没有配置,所以会报错。

如何通过示例将旧版 C# 转换为 C# 12

随着 C# 的不断发展,每个新版本都会引入强大的新功能,从而提高语言的功能和可读性。通过从旧版本的 C# 迁移到 C# 12,您可以获得更高效、更易于维护和更具表现力的代码。 由于代码库遗留、公司限制以及对旧语言功能的熟悉,许多开发人员仍在使用旧版本的 C#。升级似乎很困难,但现代版本的 C# 具有显著的优势,例如更好的性能、增强的功能和更高的安全性。 通过增量重构、试点项目和团队培训逐步

示例:推荐一个基于第三方开源控件库DataGridFilter封装的FilterColumnDataGrid,可以像Excel拥有列头筛选器

一、目的:基于第三方开源控件库DataGridFilter封装的FilterColumnDataGrid,可以像Excel拥有列头筛选器,感兴趣的可以去下方链接地址查看开源控件库地址。本控件封装的目的在于将第三方库的皮肤和样式封装到皮肤库中可统一设置样式,同时生成nuget方便调用 二、效果如下 三、环境 VS2022 Net7 四、使用方式 1、安装nuget包:H.Con