Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. DBus reply data accessed via QString
Forum Updated to NodeBB v4.3 + New Features

DBus reply data accessed via QString

Scheduled Pinned Locked Moved Solved General and Desktop
dbusmethodqstringqvariantqlist
22 Posts 3 Posters 13.5k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A amonR2

    @micland
    The name of the player is "Clementine" and I am using Qt 4.8 . No worries, thank you a lot for your help anyway. I will keep looking for a solution. If I can't find anything I will use one of the other APIs. Cheers again.

    miclandM Offline
    miclandM Offline
    micland
    wrote on last edited by
    #21

    @amonR2
    out of curiousity I was looking for a solution (I didn't know before that maps and structs are supported by DBus). And I found a way - using Qt 5.6, but should work with Qt 4.8 as well:

        // first the same code as you did already
        QDBusInterface *iface_Clementine = new QDBusInterface("org.mpris.clementine", "/Player", "org.freedesktop.MediaPlayer", QDBusConnection::sessionBus());
        if(iface_Clementine->isValid()) {
            QDBusMessage replyClementine = iface_Clementine->call("GetMetadata");
            qDebug() << replyClementine;
            // there is just one argument and this argument is a map
            QList<QVariant> args = replyClementine.arguments();
            const QDBusArgument &arg = args[0].value<QDBusArgument>();
            // the map has to be extracted entry by entry which is a tuple of key and value
            // in this case, the key is a string but the type of the value depends on the key (string, int, ...)
            arg.beginMap();
            while (!arg.atEnd()) {
                QString key;
                QDBusVariant value;
                arg.beginMapEntry();
                arg >> key >> value;
                arg.endMapEntry();
                qDebug() << "Key:" << key << "\t\tValue:" << value.variant();
            }
            arg.endMap();
        }
    

    You still have to inspect the type of the value (depends on what the entry represents: album or track number, or whatever). And the argument parsing can be wrapped in a stream operator, see http://doc.qt.io/qt-5/qdbusargument.html#beginMap-1

    Some further helpul information can be found here: http://stackoverflow.com/questions/20206376/how-do-i-extract-the-returned-data-from-qdbusmessage-in-a-qt-dbus-call

    I have no clue why the qdbusviewer of Qt 5.6 is unable to call the DBus method but the code snippet above is working fine.

    A 1 Reply Last reply
    1
    • miclandM micland

      @amonR2
      out of curiousity I was looking for a solution (I didn't know before that maps and structs are supported by DBus). And I found a way - using Qt 5.6, but should work with Qt 4.8 as well:

          // first the same code as you did already
          QDBusInterface *iface_Clementine = new QDBusInterface("org.mpris.clementine", "/Player", "org.freedesktop.MediaPlayer", QDBusConnection::sessionBus());
          if(iface_Clementine->isValid()) {
              QDBusMessage replyClementine = iface_Clementine->call("GetMetadata");
              qDebug() << replyClementine;
              // there is just one argument and this argument is a map
              QList<QVariant> args = replyClementine.arguments();
              const QDBusArgument &arg = args[0].value<QDBusArgument>();
              // the map has to be extracted entry by entry which is a tuple of key and value
              // in this case, the key is a string but the type of the value depends on the key (string, int, ...)
              arg.beginMap();
              while (!arg.atEnd()) {
                  QString key;
                  QDBusVariant value;
                  arg.beginMapEntry();
                  arg >> key >> value;
                  arg.endMapEntry();
                  qDebug() << "Key:" << key << "\t\tValue:" << value.variant();
              }
              arg.endMap();
          }
      

      You still have to inspect the type of the value (depends on what the entry represents: album or track number, or whatever). And the argument parsing can be wrapped in a stream operator, see http://doc.qt.io/qt-5/qdbusargument.html#beginMap-1

      Some further helpul information can be found here: http://stackoverflow.com/questions/20206376/how-do-i-extract-the-returned-data-from-qdbusmessage-in-a-qt-dbus-call

      I have no clue why the qdbusviewer of Qt 5.6 is unable to call the DBus method but the code snippet above is working fine.

      A Offline
      A Offline
      amonR2
      wrote on last edited by
      #22

      @micland

      WOOW!!! Thank you sooo much micland!! It works!!!
      I tried a code similar to yours but some data were missing. Just needed to do a very tiny modification on your code:

      qDebug() << key << value.variant().toString();
      

      or directly

      qDebug() << value.variant().toString();
      

      Except it, it is perfect! Thank you again.
      @micland said:

      I have no clue why the qdbusviewer of Qt 5.6 is unable to call the DBus method but the code snippet above is working fine.

      I hope it is going to change as I intend to use it or the 5.7 version very soon.

      Cheers again.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved