我试图在2个简单的应用程序之间的Qt中跨Dbus会话总线的QVariant内发送一个自定义类("Span")。Span是一个包含2个双类型属性的简单类。我已经成功地发送并恢复了一个QVariant,该QVariant仅包含一个QString,横跨dbus接口,与我下面尝试使用自定义类的QVariant的方式相同。
Span包含了类头文件中QMETATYPE QVariant注册的以下声明:
Q_DECLARE_METATYPE(Span)
我有2个测试应用程序,一个发送方和一个接收方-两者都有完全相同的'Span'类定义。在sender app中,我这样做:
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;
'setSetting'方法定义为:
inline QDBusPendingReply<int> setSetting(const QDBusVariant setting)
{
QList<QVariant> argumentList;
argumentList << QVariant::fromValue(setting);
return asyncCallWithArgumentList(QLatin1String("setSetting"), argumentList);
}
在接收器中,我像这样注册'Span'类:
qRegisterMetaType<Span>();
qDBusRegisterMetaType<Span>();
,然后我尝试恢复Span类,像这样:
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;
上面的代码为Span类的属性提供了伪值:
Setting Span to -- Low: 1.44144e-305 High: 5.24729e-261
我做错了什么?我可以在发送方应用程序中编码和解码Span实例,但是一旦接收方类通过dbus获得它,我就会获得虚假值。我非常感谢任何建议/帮助!
注意:我在我的Span类中实现了流操作符,如下所示(相同的类实现存在于接收器和发送器应用程序中):
// 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;
}
我也有同样的问题。一定有Span stuff = qdbus_cast<Span>(test);