惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hugging Face - Blog
Hugging Face - Blog
Jina AI
Jina AI
宝玉的分享
宝玉的分享
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
J
Java Code Geeks
博客园 - 【当耐特】
小众软件
小众软件
博客园 - Franky
S
SegmentFault 最新的问题
WordPress大学
WordPress大学
雷峰网
雷峰网
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
Last Week in AI
Last Week in AI
博客园_首页
月光博客
月光博客
IT之家
IT之家
阮一峰的网络日志
阮一峰的网络日志
Webroot Blog
Webroot Blog
Stack Overflow Blog
Stack Overflow Blog
腾讯CDC
云风的 BLOG
云风的 BLOG
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Recent Commits to openclaw:main
Recent Commits to openclaw:main
D
Docker
The Last Watchdog
The Last Watchdog
有赞技术团队
有赞技术团队
Hacker News - Newest:
Hacker News - Newest: "LLM"
D
DataBreaches.Net
S
Security @ Cisco Blogs
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
TaoSecurity Blog
TaoSecurity Blog
S
Security Affairs
Y
Y Combinator Blog
O
OpenAI News
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
K
Kaspersky official blog
Cloudbric
Cloudbric

夏叶随风 – 魔帆博客

QDBus之路----------Polkit-Qt5 | 魔帆博客 校园网客户端 for linux解决方案 | 魔帆博客 为Linux应用说句公道话 | 魔帆博客 JetBrains全家桶翻译插件 | 魔帆博客 漫画趣解 Linux 内核构造 | 魔帆博客 chrome-dev v58以上版本开启自带flash插件 | 魔帆博客 用c实现动态数组(int)精简版 | 魔帆博客
QDBus之路----------Hello DBus | 魔帆博客
夏叶随风 · 2017-05-23 · via 夏叶随风 – 魔帆博客

DBus基础就不说了,自行谷歌

此系列(虽然可能不会有几个)都是用cmake构建项目(ide个人倾向于使用Clion),并且环境都是linux环境下

创建一个项目(daemon———dbus服务端)

CMakeLists文件配置

在合适位置加入下列东西

#qt
set(CMAKE_AUTOMOC ON)
find_package(Qt5Widgets)
find_package(Qt5DBus)

在 add_executable(……………..)下加入

target_link_libraries(xxx Qt5::Widgets Qt5::DBus)

xxx对应你项目最终生成的可执行文件名,也就是add_executable(xxx …….)里对应的那个xxx

新建名叫Dbus的类

dbus.cpp

#include "dbus.h"

dbus::dbus(QObject *parent) :
    QObject(parent){

}

QString dbus::echo() {
    return tr("Hello DBus");
}

dbus.h

#ifndef QT_EXAMPLE_DBUS_H
#define QT_EXAMPLE_DBUS_H


#include <QtCore/QObject>

class dbus : public QObject{
    Q_OBJECT
    Q_CLASSINFO("D-Bus Interface","com.polkit.qt.example.dbus")

public:
    dbus(QObject* parent=0);


public Q_SLOTS:
    QString echo();
};


#endif   //QT_EXAMPLE_DBUS_H

调用qdbuscpp2xml来生成对应的xml文件(方便提供给别人调用你的dbus接口)

源码所在目录下运行下面命令

qdbuscpp2xml -M -s dbus.h -o com.qt.example.dbus.xml

运行qdbusxml2cpp生成对应的DBus适配器和dbus接口文件(下面用到)

生成适配器

qdbusxml2cpp com.qt.example.dbus.xml -a dbus_adaptor

生成接口

qdbusxml2cpp com.qt.example.dbus.xml -p dbus_interface

主函数中

dbus *dbus1 = new dbus; 
new DbusAdaptor(dbus1); //创建适配器
QDBusConnection connection = QDBusConnection::sessionBus();  //获取session bus
connection.registerObject("/", dbus1); //注册dbus
connection.registerService("com.polkit.qt.example.dbus"); //注册服务名

再创建一个项目(dbus客户端)

吧上面生成的接口文件拿过来

接下来自行看源码吧,很简单

至于为了不用qt5_add_dbus_interface和qt5_add_dbus_adaptor来生成那两东西,其实这两我不会用,死活生成不出来,也没报错

源码下载

QDBus之路———-Polkit-Qt5