C++ 如何在Qt DBus调用中从QDBusMessage中提取返回的数据?
我试图使用Qt的QDBus调用WPA请求者的DBus接口 类库。特别是,我试图使用“Get”属性 调用以检索“接口”属性值 “Get”的DBus规范(通过内省)是: 印刷品:C++ 如何在Qt DBus调用中从QDBusMessage中提取返回的数据?,c++,dbus,qtdbus,qdbus,C++,Dbus,Qtdbus,Qdbus,我试图使用Qt的QDBus调用WPA请求者的DBus接口 类库。特别是,我试图使用“Get”属性 调用以检索“接口”属性值 “Get”的DBus规范(通过内省)是: 印刷品: Argument 0 type is QVariant::QDBusMessage QVariant(QDBusVariant, ) QDBusArgument current type is 2 "/fi/w1/wpa_supplicant1/Interfaces/7" 如何提取实际的消息数据?我发现最简单的方法
Argument 0 type is QVariant::QDBusMessage
QVariant(QDBusVariant, )
QDBusArgument current type is 2
"/fi/w1/wpa_supplicant1/Interfaces/7"
如何提取实际的消息数据?我发现最简单的方法是使用
qDebug()
在运行时打印结果。这通常会指向下一个需要转换为的类型,直到最终达到最内部的类型
Qdbusviewer是一个有用的工具,用于确定
这将是必需的。在这种情况下:
- WPAS服务:“fi.w1.wpa_请求人1”
- WPAS路径:“/fi/w1/wpa\u请求者1”
- 属性接口标识符:“org.freedesktop.DBus.Properties”
- WPAS接口标识符:“fi.w1.wpa\U请求1”
Get
的QDBusInterface
时,我们需要使用
Properties
接口,因为该接口提供了Get
方法
在使用QDBusInterface::call()
方法调用Get
时,第二个
第三个参数与中列出的参数相对应
内省输出(“接口”
和“propname”
)<代码>“接口”是
其中可以找到属性,对于“Interfaces”
属性
是“fi.w1.wpa_requlicant1”
(可以使用qdbusviewer进行确认)
“propname”
参数只是属性的名称:
“接口”
在这种情况下
迄今为止的守则:
std::string getInterface()
{
QDBusInterface interface( "fi.w1.wpa_supplicant1",
"/fi/w1/wpa_supplicant1",
"org.freedesktop.DBus.Properties",
QDBusConnection::systemBus() );
// Calls DBus method
QDBusMessage result = interface.call( "Get",
"fi.w1.wpa_supplicant1",
"Interfaces" );
这是最难的部分QDBusInterface::call()
返回一条QDBusMessage
,
里面有我们的财产信息
qDebug() << result;
看起来不错。“ObjectPath”是我们所追求的,而且它肯定在
在某个地方
接下来我们需要QDBusMessage::arguments()
,它“返回
将要从D-Bus发送或接收的参数
返回一个QList
这个“符号”有点不清楚(括号是否表示列表?)
继续
QVariant first = outArgs.at(0);
qDebug() << first;
因此,外括号似乎表示一个数组,但为什么会有
在内部集合中使用逗号,而在外部集合中不使用逗号,这有点奇怪
一个谜
我们在遇到类型时不断转换它们:
QDBusVariant dbvFirst = first.value<QDBusVariant>();
//qDebug() << dbvFirst; // compile error!
我们似乎在兜圈子,但打印输出有点小
这次不同:
QVariant(QDBusArgument, )
另一个转换:
QDBusArgument dbusArgs = vFirst.value<QDBusArgument>();
印刷品:
Argument 0 type is QVariant::QDBusMessage
QVariant(QDBusVariant, )
QDBusArgument current type is 2
"/fi/w1/wpa_supplicant1/Interfaces/7"
2表示ArrayType
根据QDBusArgument
文档,我们可以提取
使用如下代码的数组元素:
QDBusObjectPath path;
dbusArgs.beginArray();
while (!dbusArgs.atEnd())
{
dbusArgs >> path;
// append path to a vector here if you want to keep it
}
dbusArgs.endArray();
我假设数组元素类型是QDBusObjectPath
,因为此时
这样做是有道理的。如果我是对的,就很清楚了
如果收到错误消息QDBusArgument:write from a read-only object
,请将dbusArgs
的声明更改为:
const QDBusArgument &dbusArgs = vFirst.value<QDBusArgument>();
印刷品:
Argument 0 type is QVariant::QDBusMessage
QVariant(QDBusVariant, )
QDBusArgument current type is 2
"/fi/w1/wpa_supplicant1/Interfaces/7"
终于 我的目标是获取由
fi.w1.wpa_supplicant1
接口的GetInterface
方法返回的对象路径
@马修德的回答确实有助于我开始这个实验,但不幸的是,它并没有按要求对我起作用。我尽了一切可能。但最终,不知何故,我以一种不同的、更短的方式获得了我所要求的结果。
我所做的是:-我有界面:
QDBusInterface interface interface(“fi.w1.wpa_-supplicant1”、“fi.w1.wpa_-supplicant1”、“fi.w1.wpa_-supplicant1”、QDBusConnection::systemBus())代码>
-调用该方法并存储消息
QDBusMessage mesg=interface.call(“GetInterface”、“wlan0”)代码>
-然后获取第一个参数
QVariant var=mesg.arguments().at(0)代码>
-然后获取对象路径
QDBusObjectPath objpath=var.value()代码>
-最后
QString path_str=objpath.path()代码>
现在将路径打印为字符串:
printf(“对象路径:%s\n”,路径\u str)代码>好吧,过了这么长时间
我想在调试了回复并看到它的QDBusVariant
之后
QDBusVariant
结果的variant()
方法将QDBus变量作为QVariant
对象返回。。因此,呼吁:
const auto &resultArg = result.arguments().at(0).value<QDBusVariant>().variant();
const auto&resultArg=result.arguments().at(0.value().variant();
返回一个QVariant
。。我们可以在调试中轻松打印或转换为对象中的存储值。谢谢@Zimano!当时我花了很长时间才弄明白,所以我决定把它记录在这里。