How to pack complex data into a QtDBus reply?

1.3k views Asked by At

I have to implement a DBus service in Qt that must return a reasonably complex piece of data that follows a given spec. The data is essentially a series of tested maps, arrays, structures, and other variants.

I can't find a handy way to pack all this into the reply from my method. It seems like I should be able to use the QDBusArgument class for this. For example, to create an array of strings, I should be able to do this:

QDBusArgument arg;
arg.beginArray( qMetaTypeId<QString>());
arg << "Hello" << "World";
arg.endArray();
QVariant var = arg.asVariant();

But that doesn't work. It seems I need to painstakingly construct QLists of variants manually. Surely there's a better way?

1

There are 1 answers

0
Dmitry On

I assume something like this should help you

QDBusMessage msg = QDBusMessage::createSignal( ... )
QStringList strlist;
strlist << "Hello" << "World";
msg << QVariant::fromValue( strlist);
QDBusConnection::systemBus().send( msg );

Note: If you are working with custom types, you should use the Q_DECLARE_METATYPE() macro to register your custom type.