问题
I'm trying to send a custom class ( "Span" ) inside a QVariant across the Dbus session bus in Qt between 2 simple applications. 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!
NOTE: I did implement the streaming operators in my Span class as follows ( the same class implementation is present in both the receiver and sender app ):
// Simple getters for the low and high temps
double Span::getHighTemp() const { return m_high; }
double Span::getLowTemp() const { return m_low; }
// 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_high = high;
span.m_low = low;
return argument;
}
来源:https://stackoverflow.com/questions/14086545/qt-qdbus-sending-custom-types-with-qvariant