PDA

View Full Version : Qt QDbus Sending Custom Types with QVariant



phil999
30th December 2012, 15:48
I'm trying to send a custom class ( "Span" ) inside a QVariant across the Dbus session bus in Qt between 2 simple applications. I am using Qt Version 4.8.1 ( but see the same results with 4.8.3 ) Span is a simple class that contains 2 double type properties. I have successfully sent and recovered a QVariant containing just a QString across the dbus interface in the same manner I am trying to do below with a QVariant of a custom class.

Span contains the following declaration for the QMETATYPE QVariant registration in the class header file:


Q_DECLARE_METATYPE(Span)

I have 2 test applications, one sender and one receiver - both have exactly the same 'Span' class definitions. In my sender app I do this:


qDebug() << "Sending QVariant Span to receiver...";
//int spanID = QMetaType::type("Span");
Span span(100,0.5);
//QVariant settingVariant(spanID, &span);
//QVariant settingVariant(QString("HELLO"));
QVariant settingVariant;
settingVariant.setValue(span);

QDBusVariant setting( settingVariant );
response = client->setSetting(setting);
qDebug() << "RESPONSE: " << response;

QVariant result = setting.variant(); // THIS WORKS - I can just extract my 'Span' here with the correct property values set
QVariant test = QVariant::fromValue(result);
Span testSpan = test.value<Span>();
qDebug() << "Setting Span to -- Low: " << testSpan.m_lowTemp << "High: " << testSpan.m_highTemp;

The 'setSetting' method is defined as:


inline QDBusPendingReply<int> setSetting(const QDBusVariant setting)
{
QList<QVariant> argumentList;
argumentList << QVariant::fromValue(setting);
return asyncCallWithArgumentList(QLatin1String("setSetting"), argumentList);
}

In the receiver, I register the 'Span' class like this:


qRegisterMetaType<Span>();
qDBusRegisterMetaType<Span>();

and then I attempt to recover the Span class like so:


int DbusServerTemplate::setSetting( const QDBusVariant &setting ) {

QVariant result = setting.variant();
QVariant test = QVariant::fromValue(result);
Span stuff = test.value<Span>();
qDebug() << "Setting Span to -- Low: " << stuff.m_low << "High: " << stuff.m_high;

The above code gives me bogus values for the Span class properties:


Setting Span to -- Low: 1.44144e-305 High: 5.24729e-261

What am I doing wrong? I can encode and decode the Span instance in the Sender app but once the receiver class gets it over dbus I'm getting bogus values. I'd really appreciate any ideas / help!

anda_skoa
30th December 2012, 17:21
Did you implement the streaming operators for converting your type from and to QDBusArgument?

E.g. see example here http://qt-project.org/doc/qt-4.8/qdbusargument.html#details

Cheers,
_

phil999
30th December 2012, 19:31
Hi there, yes I did here they are - I can extract the properties on the sender application after constructing my Span instance, but just not after receiving it via DBus in the receiver app.


// Simple getters for the low and high temps
double Span::getHighTemp() const { return m_highTemp; }
double Span::getLowTemp() const { return m_lowTemp; }

// Marshall the Data data into a D-Bus argument
QDBusArgument &operator<<(QDBusArgument &argument, const Span &span)

{

argument.beginStructure();

double high = span.getHighTemp();
double low = span.getLowTemp();
argument << high;
argument << low;
argument.endStructure();

return argument;

}

// Retrieve the Data data from the D-Bus argument
const QDBusArgument &operator>>(const QDBusArgument &argument, Span &span)

{

double high, low;

argument.beginStructure();
argument >> high;
argument >> low;
argument.endStructure();
span.m_highTemp = high;
span.m_lowTemp = low;

return argument;

}