[Interest] Get QtDbus interface name?

Nate Rogers bob119er at yahoo.com
Fri Sep 19 21:32:09 CEST 2014


I added protected QDBusContext to my .h file and now I get the ability to access message.service(), which is what I believe I needed.  The problem however is I am getting a segfault when I try to print the message.service value.  Am I using QDBusContext correctly?  Thanks!

ServiceData.h
---------------------------------------------------------------------
#ifndef SERVICEDATA_H
#define SERVICEDATA_H

#include <qt5/QtCore/QCoreApplication>
#include <qt5/QtDBus/QtDBus>
#include "ServiceDataAdaptor.h"

class ServiceData : public QObject, protected QDBusContext
{
    Q_OBJECT
    Q_CLASSINFO("D-Bus Interface", "com.GDM.ServiceData")

    Q_PROPERTY(quint8 ocssCount READ ocssCount WRITE setOcssCount NOTIFY ocssCountChanged)

public:
    quint8 ocssCount();
    void setOcssCount(quint8 count);

signals:
    void ocssCountChanged();

private:
    void initVariables();

    quint8 m_OcssCount;
};

#endif // SERVICEDATA_H
---------------------------------------------------------------------


ServiceData.cpp
---------------------------------------------------------------------
#include "ServiceData.h"

ServiceData::ServiceData(QObject *parent) : QObject(parent)
{
    initVariables();

    new ServiceDataAdaptor(this);
    QDBusConnection connection = QDBusConnection::systemBus();

    if (!connection.interface()->isServiceRegistered("com.GDM.ServiceData"))
    {
        if (!connection.registerService("com.GDM.ServiceData"))
        {
            qFatal("Could not register com.GDM.ServiceData service!");
        }

        if (!connection.registerObject("/ServiceData", this))
        {
            qFatal("Could not register ServiceData object!");
        }
    }
}

void ServiceData::initVariables()
{
    m_OcssCount = 12;
}

quint8 ServiceData::ocssCount()
{
    return m_OcssCount;
}

void ServiceData::setOcssCount(quint8 count)
{
    // Has the value changed?
    if (count != ocssCount())
    {
        qDebug() << "Service name: " << message().service();

        // Yes, update the value
        m_OcssCount = count;

        // Emit the value changed signal
        emit ocssCountChanged();
    }
}
---------------------------------------------------------------------

CanData.h
---------------------------------------------------------------------
#ifndef CANDATA_H
#define CANDATA_H

#include <qt5/QtDBus/QtDBus>
#include "ServiceDataInterface.h"

class CanData : public QObject, protected QDBusContext
{
    Q_OBJECT

public:
    explicit CanData(QObject *parent = 0);

private slots:
    void generateTestData();

private:
    com::GDM::ServiceData *m_ServiceData;
    QTimer *m_Timer;
};

#endif // CANDATA_H
---------------------------------------------------------------------

CanData.cpp
---------------------------------------------------------------------
#include <qt5/QtCore/QCoreApplication>
#include <qt5/QtDBus/QtDBus>
#include "CanData.h"

CanData::CanData(QObject *parent) : QObject(parent)
{
    m_ServiceData = new com::GDM::ServiceData("com.GDM.ServiceData", "/ServiceData", QDBusConnection::systemBus(), this);

    // Setup the timer
    m_Timer = new QTimer(this);
    connect(m_Timer, SIGNAL(timeout()), this, SLOT(generateTestData()));
    m_Timer->start(800);
}

void CanData::generateTestData()
{
    static int count = 0;

    count++;

    m_ServiceData->setOcssCount(count);
}
---------------------------------------------------------------------

ServiceData.xml
---------------------------------------------------------------------
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
  <interface name="com.GDM.ServiceData">
    <property name="ocssCount" type="y" access="readwrite"/>
    <signal name="ocssCountChanged">
    </signal>
  </interface>
</node>
---------------------------------------------------------------------


Nate




On Friday, September 19, 2014 10:54 AM, Thiago Macieira <thiago.macieira at intel.com> wrote:
 


On Friday 19 September 2014 06:15:47 Bob Hill wrote:

> Is there any trick
> to getting the name of the interface that called a function via
> QtDbus?

message().service()

Assuming your object derived from QDBusContext in addition to your QObject 
hierarchy.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20140919/27176d12/attachment.html>


More information about the Interest mailing list