QTDbus, Accessing array element in dbus reply message
Hi,
Im calling a method using qt dbus, the method call returns array of integers as reply, but im not able to access the array elements.
this is my method call,
<code>
QDBusMessage reply = interface->call("ReadDbusConfigFile");
qDebug() << "reply :" << reply;
qDebug() << "reply arguments : " << reply.arguments();
qDebug() << "reply[0] :" << reply.arguments().at(0);
</code>
this is the output,
reply : QDBusMessage(type=MethodReturn, service=":1.130", signature="ai", contents=([Argument: ai {2, 4}]) )
reply arguments : (QVariant(QDBusArgument, ) )
reply[0] : QVariant(QDBusArgument, )
i want to access value inside array of intiger i.e. 2 and 4,
but using this "reply.arguments().at(0)" call im getting output as "QVariant(QDBusArgument, )"
what call i need to use to access those element.
thanks.
Re: QTDbus, Accessing array element in dbus reply message
Im able to read using below code,
<code>
QList<int> ReceivedValue;
QDBusMessage reply = interface->call("method");
if (reply.type() == QDBusMessage::ReplyMessage)
{
// If it is the reply we expect, it should be a QDBusArgument
if (reply.arguments().at(0).canConvert<QDBusArgument> ())
{
// That contains an Array of QList<int> holding our received values.
QDBusArgument myArg = reply.arguments().at(0).value<QDBusArgument>();
if (myArg.currentType() == QDBusArgument::ArrayType)
{
myArg.beginArray();
// Get memory for our resulting values.
while (!myArg.atEnd())
{
int myElement = qdbus_cast<int>(myArg);
ReceivedValue.append(myElement);
}
//myArg.endArray();
}
}
}
</code>
Re: QTDbus, Accessing array element in dbus reply message
QDBusArgument has to be declared as const and myArg.endArray() should not be in comments because the program will leak. Resources allocated in beginArray() are not freed if you don't call endArray().