The trick is, that the received QVariant
for complex types is of type QDBusArgument
. If it is possible to unmarshall to the desired target type, then the way to do it, is: qv.value<QDBusArgument>() >> someConcreteObject
Qt DBus unmarshalling when sent as QDBusVariant / QVariant
Question
Class Dummy
, derived from QObject
, registered
Q_DECLARE_METATYPE(Dummy)
qRegisterMetaType<Dummy>("Dummy");
qDBusRegisterMetaType<Dummy>();
When I send Dummy
as QDBusVariant
, the slot is correctly called, but the value (of Dummy
) is default. Have noticed that the unmarshalling operator is never called (marshalling is). Via DBusMonitor
I can see the value is sent correclty.
I assume it fails because the operator QDBusArgument &operator<<(const QDBusVariant &arg);
is called and works only against Qt standard types. If I wrap a QString
in QVariant
in QDBusVariant
it works fine.
How can solve the issue?
/*!
* \brief operator << Marshalling
* \param argument
* \param dummy
* \return
*/
QDBusArgument &operator<<(QDBusArgument &argument, const Dummy& dummy)
{
argument.beginStructure();
argument << dummy.m_number; // qint32
argument << dummy.m_string; // QString
argument.endStructure();
qDebug() << "Marshall to DBus" << dummy.m_number << dummy.m_string;
return argument;
}
/*!
* \brief operator >> unmarshalling
* \param argument
* \param dummy
* \return
*/
const QDBusArgument &operator>>(const QDBusArgument &argument, Dummy &dummy)
{
argument.beginStructure();
argument >> dummy.m_number;
argument >> dummy.m_string;
argument.endStructure();
qDebug() << "Unmarshall from DBus" << dummy.m_number << dummy.m_string;
return argument;
}
Sending:
Dummy dummy(c++, "Moep");
qDebug() << "Dummy is" << dummy;
QVariant qv;
qv.setValue(dummy);
QDBusVariant qdv = QDBusVariant(qv);
testserviceInterface.receiveVariant(qdv);
Remarks:
- Qt 5.1 Win32
- This here describes a similar issue with no answer as of posting date: Qt QDbus Sending Custom Types with QVariant
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow