QDBUS 信号传值遇到的问题。

socol 2010-08-03 08:56:03
我想通过监听devkit的DBUS消息来进行device管理,通过拦截DeviceEvent信号来获得相应信息。但是在获取ARRAY 和maps/dictionaries“Compound Types“类型时碰到问题。

devkit程序的DBUS接口:
<signal name="DeviceEvent">
<arg name="action" type="s">
<doc:doc>
<doc:summary>
<doc:para>
The OS specific event.
</doc:para>
</doc:summary>
</doc:doc>
</arg>
<arg name="subsystem" type="s">
<doc:doc>
<doc:summary>
<doc:para>
The subsystem the device belongs to.
</doc:para>
</doc:summary>
</doc:doc>
</arg>
<arg name="native_path" type="s">
<doc:doc>
<doc:summary>
<doc:para>
The OS specific native path of the device.
</doc:para>
</doc:summary>
</doc:doc>
</arg>
<arg name="device_file" type="s">
<doc:doc>
<doc:summary>
<doc:para>
The UNIX special device file for the device.
</doc:para>
</arg>
<arg name="device_file_symlinks" type="as">
<doc:doc>
<doc:summary>
<doc:para>
A list of symlinks pointing to the UNIX special device file.
</doc:para>
</doc:summary>
</doc:doc>
</arg>
<arg name="properties" type="a{ss}">
<doc:doc>
<doc:summary>
<doc:para>
A set of key/value pairs with OS specific properties for the device.
</doc:para>
</doc:summary>
</doc:doc>
</arg>

<doc:doc>
<doc:description>
<doc:para>
This signal is emitted when an event occurs on a device
such as change, add, remove.
</doc:para>
</doc:description>
</doc:doc>
</signal>


QDBUS代码:
QDBusConnection::systemBus().connect("org.freedesktop.DeviceKit",
"/org/freedesktop/DeviceKit",
"org.freedesktop.DeviceKit",
"DeviceEvent",
this,
SLOT(slotDeviceEvent(const QString &,const QString &,const QString &,const QString &, QList<QString> *, QHash<QString, QString> *)));


slotDeviceEvent()中最后两个参数加上时就拦截不到信号,删去就能拦截。可能是我传的类型不对导致,不知道最后两个参数的类型应该如何定义?
...全文
652 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
lefttime 2010-08-04
  • 打赏
  • 举报
回复
容器类,我觉得用Qt自身提供的比较合适~`
tingsking18 2010-08-04
  • 打赏
  • 举报
回复
容器类用std下的试试
tingsking18 2010-08-04
  • 打赏
  • 举报
回复
这是QDBus中类型的定义:
QDBusArgument::BasicType 0 A basic element, which is understood by QVariant. The following types are considered basic: bool, byte, short, ushort, int, uint, qint64, quint64, double, QString, QByteArray, QDBusObjectPath, QDBusSignature
QDBusArgument::VariantType 1 The variant element (QDBusVariant)
QDBusArgument::ArrayType 2 An array element, usually represented by QList<T> or QVector<T>. Note: QByteArray and associative maps are not considered arrays, even if the D-Bus protocol transports them as such.
QDBusArgument::StructureType 3 A custom type represented by a structure, like QDateTime, QPoint, etc.
QDBusArgument::MapType 4 An associative container, like QMap<Key, Value> or QHash<Key, Value>
QDBusArgument::MapEntryType 5 One entry in an associative container: both the key and the value form one map-entry type.
QDBusArgument::UnknownType -1 The type is unknown or we have reached the end of the list.
socol 2010-08-04
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 tingsking18 的回复:]

试试
QHash<Key, Value>
[/Quote]
试过了,还是不行。

这是DBUS信号发射端的代码:
dbus_g_object_register_marshaller (
devkit_client_marshal_VOID__STRING_STRING_STRING_STRING_BOXED_BOXED,
G_TYPE_NONE,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_STRV,
dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_STRING),
G_TYPE_INVALID);


client->priv->proxy = dbus_g_proxy_new_for_name (client->priv->bus,
"org.freedesktop.DeviceKit",
"/org/freedesktop/DeviceKit",
"org.freedesktop.DeviceKit");
dbus_g_proxy_add_signal (client->priv->proxy,
"DeviceEvent",
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_STRV,
dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_STRING),
G_TYPE_INVALID);
tingsking18 2010-08-04
  • 打赏
  • 举报
回复
或者是std::map
tingsking18 2010-08-04
  • 打赏
  • 举报
回复
试试
QHash<Key, Value>
socol 2010-08-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lefttime 的回复:]

引用 1 楼 socol 的回复:

原来传“QList<QString> * “不行,传“const QStringList&amp;“就OK了


传值的参数QList<QString>* 类型是没问题的, 检查一下信号-槽对应的参数是否一致?
[/Quote]
首先谢谢大家的回复。
QList<QString>*试了是不行的,
另外,关于最后一个参数QHash<QString, QString> *也是不行的,需要传const QMap<QString, QVariant>&
这样问题又来了,使用const QMap<QString, QVariant>& properties后
只能得到QMap的KEY值,而得不到VALUE值,控制台报的错误为:
QDBusDemarshaller: Found unknown D-Bus type 125 '}'

打印最后一个参数的代码:
QMap<QString, QVariant>::const_iterator propertie = properties.constBegin();
while (propertie != properties.constEnd()) {
if (propertie.value().toString().isEmpty())
ui->textEdit->append(QString("%1 %2").arg(propertie.key()).arg(QString("NULL")));
else
ui->textEdit->append(QString("%1 %2").arg(propertie.key()).arg(propertie.value().toString()));
++propertie;
}
得到的结果:
ID_TYPE NULL
ID_FS_USAGE NULL
ID_VENDOR_ENC NULL
ID_FS_LABEL_ENC NULL
...
tingsking18 2010-08-04
  • 打赏
  • 举报
回复
QList<QString>和QVector<QString>应该都可以。
是否是指针类型?
lefttime 2010-08-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 socol 的回复:]

原来传“QList<QString> * “不行,传“const QStringList&“就OK了
[/Quote]

传值的参数QList<QString>* 类型是没问题的, 检查一下信号-槽对应的参数是否一致?
vanchristin 2010-08-04
  • 打赏
  • 举报
回复
引用和指针的差别还是挺大的
socol 2010-08-04
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 tingsking18 的回复:]

或者是std::map
[/Quote]
也试过了,还是不行
参考了下:
http://www.qtforum.org/article/21764/dbus-problem-with-a-compound-type-qmap.html
发现好像别人用QVariantMap可以,我试了下,结果还是获取不到VALUE
socol 2010-08-03
  • 打赏
  • 举报
回复
原来传“QList<QString> * “不行,传“const QStringList&“就OK了

16,819

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧