如何在qt dbus呼叫中从qdbusmessage转换到QString C++

发布于 2025-01-29 06:05:35 字数 1183 浏览 4 评论 0原文

在下面的代码中,我获得了有关单元调制解调器的信息。结果是我期望的数据。现在,我需要将“结果”转换为QString,以便我可以处理数据并获取对象路径或直接提取对象路径。我已经尝试了多种转换结果的方法,但是他们要么抛出无法转换qdbusmessage错误或返回空字符串。任何人都可以指向正确的方向。预先感谢

  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

这是Qdebug的输出,这是我所期望的。

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"]}]]}]) )

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"]}]]}]) )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

再见回来 2025-02-05 06:05:35

因此,在吹牛院子里并在Google上花了几个小时后,我终于确定了“结果”中的单个参数是一个关键,价值,地图。然后,随着更多的搜索,我找到了代码来从地图中提取数据。它返回/hfp/org/bluez/hci0/dev_xx_xx_xx_xx_xx_xx_xx_xx正是我需要的。工作代码在下面。感谢大家的答复。

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();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文