How do I convert data from QDBusMessage in a Qt DBus call to a QString c++

458 views Asked by At

In the code below I am getting info about my cell modem. The result is the data that I expect. Now I need to convert "result" to a QString so I can process the data and get the object path or just extract the Object Path directly. I have tried various ways to convert result but they either throw a unable to convert qdbusmessage error or return a empty string. Can anyone point me in the right direction. Thanks in advance

  QDBusInterface interface( "org.ofono",
                                "/",
                                "org.ofono.Manager",
                                QDBusConnection::systemBus() );

      QDBusMessage result = interface.call( "GetModems");
   qDebug() << "we got a" << result ;
//the last thing I tried was
QString eventReceivedName= result.arguments().at(0).value<QString>();//makes a empty string

This is the output from qDebug and it is what I am expecting.

QDBusMessage(type=MethodReturn, service=":1.4", signature="a(oa{sv})", contents=([Argument: a(oa{sv}) {[Argument: (oa{sv}) [ObjectPath: /hfp/org/bluez/hci0/dev_XX_0D_XX_81_XX_98], [Argument: a{sv} {"Online" = [Variant(bool): false], "Powered" = [Variant(bool): false], "Lockdown" = [Variant(bool): false], "Emergency" = [Variant(bool): false], "Interfaces" = [Variant(QStringList): {}], "Features" = [Variant(QStringList): {}], "Name" = [Variant(QString): "moto g power"], "Type" = [Variant(QString): "hfp"]}]]}]) )
1

There are 1 answers

0
Freecat On

So after blowing off mowing the yard and spending a couple more hours on google I finally determined that the single argument that is in "result" is a key, value, map. Then with a little more searching I found the code to extract the data from the map. It returns /hfp/org/bluez/hci0/dev_XX_XX_XX_XX_XX_XX Exactly what I needed. The working code is below. Thanks for the replies everyone.

QDBusInterface interface( "org.ofono",
                                    "/",
                                    "org.ofono.Manager",
                                    QDBusConnection::systemBus() );

        QDBusMessage result = interface.call( "GetModems");

        QList<QVariant> args = result.arguments();
               const QDBusArgument &arg = args[0].value<QDBusArgument>();

               arg.beginMap();
               while (!arg.atEnd()) {
                   QString key;
                   QDBusVariant value;
                   arg.beginMapEntry();
                   arg >> key >> value;
                   arg.endMapEntry();

                   qDebug() << key;//could get the value as well with value.variant() 
               }
               arg.endMap();