博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
QT QDbus
阅读量:4075 次
发布时间:2019-05-25

本文共 2127 字,大约阅读时间需要 7 分钟。

通过Dbus 总线,客户端调用服务端的槽函数

服务端

1.新建项目QdbusTestServer

在.pro工程中增加QT += dbus

2.新建类 Cal定义接口和函数add(),sub()

定义接口名com.example.dbus.Interface

Q_CLASSINFO(“D-Bus Interface”,”com.example.dbus.Interface”)

cal.h#include "qobject.h"class Cal : public QObject{    Q_OBJECT    Q_CLASSINFO("D-Bus Interface","com.example.dbus.Interface")public:    Cal();public slots:    int add(int,int);    int sub(int,int);};cal.cpp#include "cal.h"Cal::Cal() :QObject(){}int Cal::add(int a, int b){    return a+b;}int Cal::sub(int a, int b){    return a-b;}

3.生成xml接口文件,生成Adapter,Interface文件

qdbuscpp2xml -M cal.h -o com.example.dbus.xml

qdbusxml2cpp com.example.dbus.xml -a cal_adaptor
qdbusxml2cpp com.example.dbus.xml -p cal_interface
将生成的文件加到工程中
这里写图片描述

4.服务端创建QDBusConnection对象,注册服务和对象

  Cal *cal = new Cal();    new InterfaceAdaptor(cal);    QDBusConnection connection = QDBusConnection::sessionBus();//注册服务名    connection.registerService("com.example.dbus");    //注册对象名    connection.registerObject("/", cal);

运行QT程序,用q-dfeet检查服务是否注册成功

这里写图片描述
测试:
~$ dbus-send –session –print-reply –dest=com.example.dbus / com.example.dbus.Interface.add int32:5 int32:6
method return sender=:1.1707 -> dest=:1.1710 reply_serial=2
int32 11

客户端调用服务端的函数

1.新建工程QdbusTestClient

新建工程QdbusTestClient

在.pro中增加QT += dbus
增加要访问的接口文件cal_interface.h cal_interface.cpp

2.在main函数中

QDBusConnection connection = QDBusConnection::sessionBus();    com::example::dbus::Interface *iface;//用interface新建一个iface接口//参数:com.example.dbus 是服务端创建的服务名,参数:/ 为服务端创建的Object Path//参数:connection 为申请的connection//参数:w.parent() 为当前对象this    iface = new com::example::dbus::Interface("com.example.dbus","/",                                   connection,w.parent()); QDBusPendingReply
posReply = iface->add(15,10);//调用服务端的接口函数 posReply.waitForFinished(); qDebug() << posReply.value(); //输出返回结果25

测试

$ dbus-monitor --session \ "type='method_call',interface='com.example.dbus.Interface'"method call sender=:1.1837 -> dest=com.example.dbus serial=9 path=/; interface=com.example.dbus.Interface; member=add   int32 15   int32 10

总结:服务端引入文件cal_adapter.h cal_adapter.cpp

客户端引入文件cal_interface.h cal_interface.cpp

你可能感兴趣的文章
《数据库系统概论》 第三章 关系数据库标准语言SQL
查看>>
SQL语句(二)查询语句
查看>>
SQL语句(六) 自主存取控制
查看>>
《计算机网络》第五章 运输层 ——TCP和UDP 可靠传输原理 TCP流量控制 拥塞控制 连接管理
查看>>
堆排序完整版,含注释
查看>>
二叉树深度优先遍历和广度优先遍历
查看>>
生产者消费者模型,循环队列实现
查看>>
PostgreSQL代码分析,查询优化部分,process_duplicate_ors
查看>>
PostgreSQL代码分析,查询优化部分,canonicalize_qual
查看>>
PostgreSQL代码分析,查询优化部分,pull_ands()和pull_ors()
查看>>
IA32时钟周期的一些内容
查看>>
获得github工程中的一个文件夹的方法
查看>>
《PostgreSQL技术内幕:查询优化深度探索》养成记
查看>>
PostgreSQL查询优化器详解之逻辑优化篇
查看>>
STM32中assert_param的使用
查看>>
C语言中的 (void*)0 与 (void)0
查看>>
vu 是什么
查看>>
io口的作用
查看>>
IO口的作用
查看>>
UIView的使用setNeedsDisplay
查看>>