在下面的代码中,我正在获取有关我的蜂窝调制解调器的信息。结果是我所期望的数据。现在我需要转换"result"到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"]}]]}]) )
因此,在修剪庭院并在谷歌上花费了几个小时之后,我最终确定了"result"中的单个参数是键、值、映射。然后,通过更多的搜索,我找到了从地图中提取数据的代码。它返回/hfp/org/bluez/hci0/dev_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();