#include <QObject>#include <QtCore/QObject>#include <QtDBus/QDBusAbstractAdaptor>#include <QtDBus/QDBusVariant>class CListener : public QDBusAbstractAdaptor{ Q_OBJECT Q_CLASSINFO("D-Bus Interface", "com.mycompany.hangapp.service") Q_PROPERTY(QString message_text READ messageText WRITE setMessageText) QString fmessageText;public: explicit CListener(QObject *parent = 0); QString messageText() const; void setMessageText(const QString & value);signals: void aboutToQuit();public slots: QDBusVariant query(const QString & query); Q_NOREPLY void quit(); };
#include "clistener.h"#include <QTimer>CListener::CListener(QObject *parent) : QDBusAbstractAdaptor(parent){}QString CListener::messageText() const{ return fmessageText;}void CListener::setMessageText(const QString &value){ fmessageText = value;}void CListener::quit(){// QTimer::singleShot(0,QCoreApplication::instance(), SLOT(quit()));}QDBusVariant CListener::query(const QString &query){ QString answer = "Answer :"+query; return QDBusVariant(answer);}
#include <stdio.h>#include <QCoreApplication>#include <QtDBus/QtDBus>#include "clistener.h"#include "hangapp_service.h"using namespace std;int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); QObject object; printf("Mycompany hangapp service 0.1\n"); printf("Try to register in D-Bus "); CListener * listener = new CListener(&object); listener->setProperty("message_text","init_value"); QDBusConnection::sessionBus().registerObject("/",&object); if(!QDBusConnection::sessionBus().registerService(SERVICE_NAME)) { printf("failed with reason '%s'\n",qPrintable(QDBusConnection::sessionBus().lastError().message())); exit(1); } else { printf("ok\nService name is %s\n",SERVICE_NAME); } return a.exec();}
#include <QMainWindow>#include <QtDBus/QDBusInterface>namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{ Q_OBJECT QDBusInterface *iface; void checkDBusInterface(); public: explicit MainWindow(QWidget *parent = 0); ~MainWindow();private: Ui::MainWindow *ui;private slots: void on_pushButton_clicked();public slots: void dbus_start_notify(const QString & dbus_name); void dbus_stop_notify(const QString & dbus_name);};
#include "mainwindow.h"#include "ui_mainwindow.h"#include <QtDBus/QtDBus>#include "hangapp_service.h"MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); iface = NULL; connect(QDBusConnection::sessionBus().interface(),SIGNAL(serviceRegistered(QString)),this,SLOT(dbus_start_notify(QString))); connect(QDBusConnection::sessionBus().interface(),SIGNAL(serviceUnregistered(QString)),this,SLOT(dbus_stop_notify(QString))); checkDBusInterface();}void MainWindow::checkDBusInterface(){ iface = new QDBusInterface(SERVICE_NAME, "/", "com.mycompany.hangapp.service", QDBusConnection::sessionBus(), this); if (!iface->isValid()) { fprintf(stderr, "%s\n", qPrintable(QDBusConnection::sessionBus().lastError().message())); ui->pushButton->setEnabled(false); ui->lineEdit->setEnabled(false); ui->listWidget->setEnabled(false); delete iface; iface = NULL; } else { ui->pushButton->setEnabled(true); ui->lineEdit->setEnabled(true); ui->listWidget->setEnabled(true); }}MainWindow::~MainWindow(){ delete ui;}void MainWindow::on_pushButton_clicked(){ QDBusReply<QDBusVariant> reply = iface->call("query", ui->lineEdit->text()); if(reply.isValid()) ui->listWidget->addItem(qPrintable(reply.value().variant().toString()));}void MainWindow::dbus_start_notify(const QString &dbus_name){ if(dbus_name==SERVICE_NAME) { checkDBusInterface(); }}void MainWindow::dbus_stop_notify(const QString &dbus_name){ if(dbus_name==SERVICE_NAME) { ui->pushButton->setEnabled(false); ui->lineEdit->setEnabled(false); ui->listWidget->setEnabled(false); delete iface; iface = NULL; }}
#include "mainwindow.h"#include <QApplication>#include <QtDBus>int main(int argc, char *argv[]){ QApplication a(argc, argv); if (!QDBusConnection::sessionBus().isConnected()) { fprintf(stderr, "Cannot connect to the D-Bus session bus.\n" "To start it, run:\n" "\teval `dbus-launch --auto-syntax`\n"); return 1; } MainWindow w; w.show(); return a.exec();}