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

推荐订阅源

WordPress大学
WordPress大学
V
Visual Studio Blog
P
Privacy International News Feed
月光博客
月光博客
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
Lohrmann on Cybersecurity
N
News and Events Feed by Topic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
Webroot Blog
Webroot Blog
T
Threatpost
宝玉的分享
宝玉的分享
The Last Watchdog
The Last Watchdog
小众软件
小众软件
L
LINUX DO - 最新话题
C
Cisco Blogs
T
Troy Hunt's Blog
Schneier on Security
Schneier on Security
酷 壳 – CoolShell
酷 壳 – CoolShell
www.infosecurity-magazine.com
www.infosecurity-magazine.com
雷峰网
雷峰网
G
GRAHAM CLULEY
有赞技术团队
有赞技术团队
Know Your Adversary
Know Your Adversary
博客园 - 叶小钗
罗磊的独立博客
V
V2EX
博客园 - Franky
P
Proofpoint News Feed
SecWiki News
SecWiki News
腾讯CDC
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
博客园 - 三生石上(FineUI控件)
S
Secure Thoughts
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
PCI Perspectives
PCI Perspectives
V2EX - 技术
V2EX - 技术
Google DeepMind News
Google DeepMind News
Last Week in AI
Last Week in AI
aimingoo的专栏
aimingoo的专栏
Cisco Talos Blog
Cisco Talos Blog
N
News and Events Feed by Topic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
SegmentFault 最新的问题

Qt for linux – 魔帆博客

QDBus之路----------Polkit-Qt5 | 魔帆博客
QDBus之路----------Hello DBus | 魔帆博客
夏叶随风 · 2017-05-23 · via Qt for linux – 魔帆博客

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