当前位置: 首页 > news >正文

女生自己做网站/百度搜索风云排行榜

女生自己做网站,百度搜索风云排行榜,布局设计,广州的一起做网站怎么样Q-Bus的官方例子是个遥控小车 在windows上自己手动编译了dbus,但还是跑不起来,在搜索Qt进程通信方式的时候,无意中看到Qt Remote Object(QtRO),这种方式支持跨进程的信号与槽,就尝试着用QtRO来…

Q-Bus的官方例子是个遥控小车
在这里插入图片描述
在windows上自己手动编译了dbus,但还是跑不起来,在搜索Qt进程通信方式的时候,无意中看到Qt Remote Object(QtRO),这种方式支持跨进程的信号与槽,就尝试着用QtRO来改造这个遥控小车,让它跑起来。
看看我们的运行效果:
在这里插入图片描述
最后发现用QtRO其实更好用,前提是你的Qt版本是5.9及以上,如果不是的话,考虑用QLocalSocket和QLocalServer来实现(同一台电脑,如果是不同电脑,就用QTcpServer和QTcpSocket来实现,或者其他的方式也可以实现)。

概述

Qt Remote Object简称QtRO,这是Qt5.9以后官方推出来的新模块,专门用于进程间通信(IPC)。Qt官方推出的这个新模块是基于Socket来封装的,使用起来非常方便,兼容LPC和RPC。LPC即Local Process Communication,而RPC是指Remote Process Communication,两者都属于IPC。QtRO能够工作于这两种不同的模式:如果用于LPC,则QtRO使用QLocalSocket;如果是用于RPC,则使用QTcpSocket。

QtRO

QtRO本质上是一个点对点的通信网络。每个进程通过QRemoteObjectNode接入QtRO网络。功能提供节点(可以理解为服务器)需要使用QRemoteObjectHost将一个提供实际功能的QObject派生类注册进QtRO网络中,然后其他使用该功能的程序则通过各自的QRemoteObjectNode连接到该Host上,然后acquire一个该功能对象的Replica。等到该Replica初始化好后,该程序就能够使用Replica中的信号、槽以及属性,就好像功能类就在本地一样。

关键步骤

  • 要使用QtRO有几个关键步骤,我们暂且将两个端分为Server和Client。
  • Server端需要把功能类通过QRemoteObjectHost的enableRemoting方法共享出来
  • Client连接到该QRemoteObjectHost,然后acquire到Replica
  • QtRO会自动初始化该Replica,待初始化完后客户端就可以用该Replica

将官方的小车工程复制了一份,改造后的项目结构如下(删除Dbus相关的库和配置):
在这里插入图片描述
QtRO有分两种Replica,一种是静态Replica,一种是动态Replica。我们这里采用的是静态Replica方式。

1.客户端(小车工程)

服务端和客户端的的pro文件都需要加的是

QT += remoteobjects
REPC_REPLICA += ../Reps/CommonInterface.rep

创建一个文本CommonInterface.rep
(采用UTF-8编码格式的文本)

class CommonInterface
{SIGNAL(sigMessage(const QString &msg))   //server下发消息给clientSLOT(void onMessage(const QString &msg)) //server接收client的消息
}

单独放在一个文件夹
在这里插入图片描述

car.h文件中
改造后是这样(删除DBus相关头文件和变量)

#include <QRemoteObjectNode>
#include "rep_CommonInterface_replica.h"
//...中间省略
public Q_SLOTS://省略void onReceiveMsg(const QString &msg);
private:void init();private://...其余省略QRemoteObjectNode * m_pRemoteNode = nullptr;CommonInterfaceReplica * m_pInterface = nullptr;
void Car::onReceiveMsg(const QString &msg)
{qDebug() << "onReceiveMsg:" << msg;if (msg == "accelerate") {accelerate();} else if (msg == "decelerate") {decelerate();} else if (msg == "turnLeft") {turnLeft();} else if (msg == "turnRight") {turnRight();}
}

在构造函数中调用init()函数

void Car::init()
{m_pRemoteNode = new QRemoteObjectNode(this);bool isConnected = m_pRemoteNode->connectToNode(QUrl("local:interfaces"));qDebug() << "isConnected:" << isConnected;m_pInterface = m_pRemoteNode->acquire<CommonInterfaceReplica>();m_pInterface->waitForSource(); //这里是阻塞的,待优化connect(m_pInterface, &CommonInterfaceReplica::sigMessage, this, &Car::onReceiveMsg);
}

说明:#include “rep_CommonInterface_replica.h”
这里的rep_CommonInterface_replica.h文件是自动生成的
在这里插入图片描述
m_pInterface->waitForSource(); //这里是阻塞的,待优化
为什么加这里,加了这里后,可以在接下来的代码直接发消息(否则你会发现界面出来后发消息没有问题,但是init函数结尾调用发消息函数,发现的消息就失败,原因是这时候还没建立好连接)

 m_pInterface = m_pRemoteNode->acquire<CommonInterfaceReplica>();
    template < class ObjectType >ObjectType *acquire(const QString &name = QString()){return new ObjectType(this, name);}

这里官方建议用智能指针来管理内存,这里也看到了new了一个对象后,不小心就内存泄漏了。

这里就先不用智能指针,代码工程中会用std::shared_ptr进行管理。

2.Server端(遥控器工程)

在控制端的pro文件中需要加

QT += widgets remoteobjects
REPC_SOURCE += ../Reps/CommonInterface.rep

添加
commoninterface.h

#ifndef COMMONINTERFACE_H
#define COMMONINTERFACE_H//找不rep_commoninterface_source.h的时候,添加这个头文件,就会自动生成rep_commoninterface_source.h
#include "rep_CommonInterface_replica.h"#include "rep_commoninterface_source.h"class CommonInterface : public CommonInterfaceSource
{Q_OBJECT
public:explicit CommonInterface(QObject * parent = nullptr);virtual void onMessage(const QString &msg);void sendMsg(const QString &msg);
signals:void sigReceiveMsg(const QString &msg);};#endif // COMMONINTERFACE_H

commoninterface.cpp

#include "commoninterface.h"
#include <QDebug>CommonInterface::CommonInterface(QObject *parent):CommonInterfaceSource(parent)
{
}/*** @brief CommonInterface::onMessage* @param msg* 接收客户端的消息*/
void CommonInterface::onMessage(const QString &msg)
{emit sigReceiveMsg(msg);
}/*** @brief CommonInterface::sendMsg* @param msg* 发送消息给客户端*/
void CommonInterface::sendMsg(const QString &msg)
{emit sigMessage(msg);
}
#include "ui_controller.h"#include "commoninterface.h"
#include <QRemoteObjectHost>class Controller : public QWidget
{Q_OBJECT//中间省略了原有的代码,仍然是删除掉DBbus相关的头文件和代码
protected:void timerEvent(QTimerEvent *event);private slots:void onReceiveMsg(const QString &msg);
private:void init();private:CommonInterface * m_pInterface = nullptr;QRemoteObjectHost * m_pHost = nullptr;
};

controller.cpp

#include <QtWidgets>#include "controller.h"Controller::Controller(QWidget *parent): QWidget(parent)
{ui.setupUi(this);
//    car = new org::example::Examples::CarInterface("org.example.CarExample", "/Car",
//                           QDBusConnection::sessionBus(), this);init();startTimer(1000);
}void Controller::timerEvent(QTimerEvent *event)
{Q_UNUSED(event)
//    if (m_pInterface->isValid())
//        ui.label->setText("connected");
//    else
//        ui.label->setText("disconnected");
}void Controller::on_accelerate_clicked()
{m_pInterface->sendMsg("accelerate");
//    car->accelerate();
}void Controller::on_decelerate_clicked()
{m_pInterface->sendMsg("decelerate");
//    car->decelerate();
}void Controller::on_left_clicked()
{m_pInterface->sendMsg("turnLeft");
//    car->turnLeft();
}void Controller::on_right_clicked()
{m_pInterface->sendMsg("turnRight");//    car->turnRight();
}void Controller::onReceiveMsg(const QString &msg)
{qDebug() << "onReceiveMsg:" << msg;
}void Controller::init()
{m_pHost = new QRemoteObjectHost(this);m_pHost->setHostUrl(QUrl("local:interfaces"));m_pInterface = new CommonInterface(this);m_pHost->enableRemoting(m_pInterface);connect(m_pInterface, &CommonInterface::sigReceiveMsg, this, &Controller::onReceiveMsg);
}

如果你也遇到说找不到rep_commoninterface_source.h
可以进行以下两种方法尝试:

方法一:点两次生成(第一次生成说找不到这个头文件,第二次就生成成功了)

方法二:
我的Qt Creator版本是5.14.1,不知道为什么,单独包含rep_commoninterface_source.h的时候,会报找不到这个头文件,于是我就客户端中自动生成的rep_CommonInterface_replica.h拷贝到了工程中,添加到commoninterface.h中,重新编译,就好了(后面从工程中删除这个rep_CommonInterface_replica.h也不会再报错)

Qt Remote Objects Compiler

另外:

完整代码在这里:
链接:https://pan.baidu.com/s/1qQK0D8OCwJHfMGerDepJfw
提取码:pdvv

参考:
Qt Remote Object(QtRO)实现进程间通信
Qt Remote Objects Compiler

http://www.lbrq.cn/news/1588159.html

相关文章:

  • 微山做网站/地推接单平台
  • 自己做网站赚钱/seo教程网站优化推广排名
  • 郑州公司做网站/电商关键词一般用哪些工具
  • 苍南配网设计/aso关键词覆盖优化
  • 昆山建设银行网站首页/seo实战培训视频
  • 怎么用wordpress 建站/百度域名收录
  • 网站开设作风建设专栏/毕业设计网站
  • WordPress浮动导航插件/搜狗搜索引擎优化
  • 网站流量盈利模式/线上宣传方式
  • 新邵县住房和城乡建设局网站/国内建站平台
  • 企业一般用哪个erp系统/广州百度搜索排名优化
  • 新疆建设厅网站/seo关键词排名优化哪家好
  • 室内设计网站都有哪些平台/搜索引擎提交入口大全
  • 响应式网站制作/清理大师
  • 网站建设用苹果系统与liunx/品牌营销策划与管理
  • qml 网站开发/备案查询站长工具
  • 网站制作里的更多怎么做/seo1视频发布会
  • 阿里巴巴的网站怎么做的/搜索引擎优化搜索优化
  • 160 作者 网站建设 amp/aso搜索排名优化
  • wordpress企业主题模板下载/长春seo排名
  • b2c旅游电子商务网站有哪些/南昌seo排名扣费
  • 禅城区企业网站建设/seo怎么做优化排名
  • 展览策划/关键词优化简易
  • 成都市网站设计开发/今日军事新闻最新消息
  • 太原做网站设计/免费seo网站推广
  • 广州黄埔网站建设公司/深圳网站制作推广
  • 内江网站建设/十大经典事件营销案例
  • 不更新网站如何做排名/十大推广app平台
  • 哪个商城网站建设好/一键优化清理加速
  • 网站模板超市/优化
  • 并发编程常见问题排查与解决:从死锁到线程竞争的实战指南
  • Hive【应用 04】常用DDL操作(数据库操作+创建表+修改表+清空删除表+其他命令)
  • 基于 kubeadm 搭建 k8s 集群
  • 探索设计模式的宝库:Java-Design-Patterns
  • VUE-第二季-02
  • ENSP防火墙安全策略简单案例