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

推荐订阅源

D
Docker
AI
AI
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
Jina AI
Jina AI
大猫的无限游戏
大猫的无限游戏
博客园 - 【当耐特】
V
Visual Studio Blog
博客园 - Franky
宝玉的分享
宝玉的分享
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
I
Intezer
C
Cybersecurity and Infrastructure Security Agency CISA
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
SegmentFault 最新的问题
腾讯CDC
T
Threat Research - Cisco Blogs
Last Week in AI
Last Week in AI
酷 壳 – CoolShell
酷 壳 – CoolShell
Webroot Blog
Webroot Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
小众软件
小众软件
C
Cyber Attacks, Cyber Crime and Cyber Security
Hacker News: Ask HN
Hacker News: Ask HN
T
Tor Project blog
WordPress大学
WordPress大学
雷峰网
雷峰网
J
Java Code Geeks
GbyAI
GbyAI
Recorded Future
Recorded Future
F
Full Disclosure
Cisco Talos Blog
Cisco Talos Blog
S
Secure Thoughts
I
InfoQ
量子位
Forbes - Security
Forbes - Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
Threatpost
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Security Blog
Microsoft Security Blog
Attack and Defense Labs
Attack and Defense Labs
爱范儿
爱范儿
N
News and Events Feed by Topic
V
Vulnerabilities – Threatpost
L
LINUX DO - 最新话题
A
Arctic Wolf
S
Security Affairs

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