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)
Q_DECLARE_METATYPE(Span)
To copy to clipboard, switch view to plain text mode
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"));
settingVariant.setValue(span);
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 Span testSpan = test.value<Span>();
qDebug() << "Setting Span to -- Low: " << testSpan.m_lowTemp << "High: " << testSpan.m_highTemp;
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;
To copy to clipboard, switch view to plain text mode
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
);
}
inline QDBusPendingReply<int> setSetting(const QDBusVariant setting)
{
QList<QVariant> argumentList;
argumentList << QVariant::fromValue(setting);
return asyncCallWithArgumentList(QLatin1String("setSetting"), argumentList);
}
To copy to clipboard, switch view to plain text mode
In the receiver, I register the 'Span' class like this:
qRegisterMetaType<Span>();
qDBusRegisterMetaType<Span>();
qRegisterMetaType<Span>();
qDBusRegisterMetaType<Span>();
To copy to clipboard, switch view to plain text mode
and then I attempt to recover the Span class like so:
int DbusServerTemplate
::setSetting( const QDBusVariant &setting
) {
Span stuff = test.value<Span>();
qDebug() << "Setting Span to -- Low: " << stuff.m_low << "High: " << stuff.m_high;
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;
To copy to clipboard, switch view to plain text mode
The above code gives me bogus values for the Span class properties:
Setting Span to -- Low: 1.44144e-305 High: 5.24729e-261
Setting Span to -- Low: 1.44144e-305 High: 5.24729e-261
To copy to clipboard, switch view to plain text mode
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!
Bookmarks