[Interest] Question about using QtDBus

Rogers Nate nate.rogers at raymondcorp.com
Thu Aug 28 19:17:54 CEST 2014


I have a data process where I am trying to store my data so it can be
updated/retrieved from different processes.  I am trying to get a QList
of a custom object to be returned from the data object so it can be used
by my CAN communication object.  I have striped down some code to give
an example of what I am trying to do.  I have been trying to get this to
work for awhile but I must be missing something and I am not fully
understanding what needs to be done.  I am thinking it has something to
do with my dbus type, I feel like the xml file should define it as a(ss)
but it won't compile with that and gives no meaningful information as to
why.  If I change it to ao it will compile but no data is available over
in the CAN comm process and it prints an error saying it expected a(ss)
before killing the process.  Any help is greatly appreciated, thanks!

Data Process...

------------------------- Begin main.cpp -------------------------
#include <qt5/QtCore/QCoreApplication>
#include <qt5/QtDBus/QtDBus>
#include "ServiceData.h"
#include "ServiceErrorLogData.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    ServiceErrorLogData::registerMetaType();

    ServiceData *serviceData = new ServiceData();
    Q_UNUSED(serviceData);

    return a.exec();
}
------------------------- End main.cpp -------------------------


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

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

    new ServiceDataAdaptor(this);
    QDBusConnection connection = QDBusConnection::systemBus();
    connection.registerObject("/ServiceData", this);
    connection.registerService("com.GDM.ServiceData");
}

void ServiceData::initVariables()
{
    ServiceErrorLogData tempData = ServiceErrorLogData("2/17   
11:14:40a", "64 95%");
    m_ServiceErrorLog.append(tempData);
    ServiceErrorLogData tempData2 = ServiceErrorLogData("2/5   
8:20:18a", "8L 53%");
    m_ServiceErrorLog.append(tempData2);
}

QList<ServiceErrorLogData> ServiceData::serviceErrorLog()
{
    return m_ServiceErrorLog;
}
-------------------------- End ServiceData.cpp ----------------------

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

#include <qt5/QtDBus/QtDBus>
#include "ServiceErrorLogData.h"
#include "servicedata_adaptor.h"

class ServiceErrorLogData;

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

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

public slots:
    QList<ServiceErrorLogData> serviceErrorLog();

private:
    void initVariables();

    QList<ServiceErrorLogData> m_ServiceErrorLog;
};

Q_DECLARE_METATYPE(QList<ServiceErrorLogData>)

#endif // SERVICEDATA_H
------------------------- End ServiceData.h -------------------------


------------------- Begin ServiceErrorLogData.cpp -------------------
#include "ServiceErrorLogData.h"

ServiceErrorLogData::ServiceErrorLogData() :
        m_Label(),
        m_Value()
{
}

ServiceErrorLogData::ServiceErrorLogData(const QString &label, const
QString &value) :
        m_Label(label),
        m_Value(value)
{
}

ServiceErrorLogData::ServiceErrorLogData(const ServiceErrorLogData &other) :
        m_Label(other.m_Label),
        m_Value(other.m_Value)
{
}

ServiceErrorLogData& ServiceErrorLogData::operator=(const
ServiceErrorLogData &other)
{
    m_Label = other.m_Label;
    m_Value = other.m_Value;

    return *this;
}

ServiceErrorLogData::~ServiceErrorLogData()
{
}

QString ServiceErrorLogData::label() const
{
    return m_Label;
}

QString ServiceErrorLogData::value() const
{
    return m_Value;
}

void ServiceErrorLogData::registerMetaType()
{
    qRegisterMetaType<ServiceErrorLogData>("ServiceErrorLogData");

    qDBusRegisterMetaType<ServiceErrorLogData>();
    qDBusRegisterMetaType<QList<ServiceErrorLogData> >();
}

QDBusArgument &operator<<(QDBusArgument &argument, const
ServiceErrorLogData &logData)
{
    argument.beginStructure();
    argument << logData.m_Label;
    argument << logData.m_Value;
    argument.endStructure();

    return argument;
}

const QDBusArgument &operator>>(const QDBusArgument &argument,
ServiceErrorLogData &logData)
{
    argument.beginStructure();
    argument >> logData.m_Label;
    argument >> logData.m_Value;
    argument.endStructure();

    return argument;
}
------------------- End ServiceErrorLogData.cpp -------------------

------------------ Begin ServiceErrorLogData.h ------------------
#ifndef SERVICEERRORLOGDATA_H
#define SERVICEERRORLOGDATA_H

class ServiceErrorLogData
{

public:
    ServiceErrorLogData();
    ServiceErrorLogData(const QString &label, const QString &value);
    ServiceErrorLogData(const ServiceErrorLogData &other);
    ServiceErrorLogData& operator=(const ServiceErrorLogData &other);
    ~ServiceErrorLogData();

    friend QDBusArgument &operator<<(QDBusArgument &argument, const
ServiceErrorLogData &logData);
    friend const QDBusArgument &operator>>(const QDBusArgument
&argument, ServiceErrorLogData &logData);

    QString label() const;
    QString value() const;

    // Register ServiceErrorLogData with the Qt type system
    static void registerMetaType();

private:
    QString m_Label;
    QString m_Value;
};

Q_DECLARE_METATYPE(ServiceErrorLogData)

#endif // SERVICEERRORLOGDATA_H
------------------- End ServiceErrorLogData.h --------------------

-------------------- Begin ServiceData.h -----------------------
<!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">
    <method name="serviceErrorLog">
        <arg name="serviceErrorLogData" type="a(ss)" direction="out"/>
    </method>
  </interface>
</node>
-------------------- End ServiceData.h -----------------------

---------------- Begin com.GDM.ServiceData.conf -------------------
<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration
1.0//EN"
 "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
    <policy user="root">
        <allow own="com.GDM.ServiceData"/>
        <allow send_type="method_call" log="true"/>
    </policy>
    <policy context="default">
        <allow send_interface="com.GDM.ServiceData"/>
        <allow receive_sender="com.GDM.ServiceData"/>
        <allow receive_interface="com.GDM.ServiceData"/>
    </policy>
</busconfig>
---------------- End com.GDM.ServiceData.conf -------------------



Comm Process...

------------------------- Begin main.cpp -------------------------
#include <qt5/QtCore/QCoreApplication>
#include <qt5/QtDBus/QtDBus>

#include "CanData.h"
#include "ServiceErrorLogData.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    ServiceErrorLogData::registerMetaType();

    CanData *canData = new CanData();
    Q_UNUSED(canData);

    return a.exec();
}
------------------------- End main.cpp -------------------------


------------------------ Begin CanData.cpp ------------------------
#include <qt5/QtCore/QCoreApplication>
#include <qt5/QtDBus/QtDBus>
#include "CanData.h"
#include "ServiceErrorLogData.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()
{
    QDBusPendingReply<QList<ServiceErrorLogData> > logData =
m_ServiceData->serviceErrorLog();
    logData.waitForFinished();
    if (logData.isError())
    {
        qDebug() << logData.error();
    }
    QList<ServiceErrorLogData> tempData = logData.value();
    qDebug() << "Log Data Count: " << tempData.count();
}
------------------------ End CanData.cpp ------------------------

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

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

class CanData : public QObject
{
    Q_OBJECT

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

private slots:
    void generateTestData();

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

#endif // CANDATA_H
------------------------ End CanData.h ------------------------


Thanks again!

Nate






Confidentiality Notice: The preceding e-mail message (including any attachments) contains information that may be confidential, protected by applicable legal privileges, or constitute non-public information. It is intended to be conveyed only to the designated recipient(s). If you are not an intended recipient of this message, please notify the sender by replying to this message and then delete it from your system. Use, dissemination, distribution or reproduction of this message by unintended recipients is not authorized and may be unlawful.




More information about the Interest mailing list