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

推荐订阅源

AI
AI
TaoSecurity Blog
TaoSecurity Blog
H
Heimdal Security Blog
Help Net Security
Help Net Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Microsoft Azure Blog
Microsoft Azure Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Google DeepMind News
Google DeepMind News
爱范儿
爱范儿
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
N
News | PayPal Newsroom
V2EX - 技术
V2EX - 技术
博客园 - 【当耐特】
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Secure Thoughts
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy & Cybersecurity Law Blog
有赞技术团队
有赞技术团队
S
Schneier on Security
S
SegmentFault 最新的问题
Google Online Security Blog
Google Online Security Blog
H
Hacker News: Front Page
The Last Watchdog
The Last Watchdog
Schneier on Security
Schneier on Security
PCI Perspectives
PCI Perspectives
IT之家
IT之家
Project Zero
Project Zero
博客园 - 司徒正美
P
Privacy International News Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Jina AI
Jina AI
Security Latest
Security Latest
Hacker News - Newest:
Hacker News - Newest: "LLM"
腾讯CDC
C
CXSECURITY Database RSS Feed - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
V
Vulnerabilities – Threatpost
W
WeLiveSecurity
NISL@THU
NISL@THU
Webroot Blog
Webroot Blog
N
Netflix TechBlog - Medium
L
Lohrmann on Cybersecurity

夏叶随风 – 魔帆博客

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