qdbusxml2cpp未知类型

7
当我使用qdbusxml2cpp程序将以下xml转换为Qt类时,出现了以下错误:
qdbusxml2cpp -c ObjectManager -a ObjectManager:ObjectManager.cpp xml/object_manager.xml 
Got unknown type `a{oa{sa{sv}}}'
You should add <annotation name="com.trolltech.QtDBus.QtTypeName.Out0" value="<type>"/> to the XML description

D-Feet描述:

在此输入图片描述

XML:

<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node><interface name="org.freedesktop.DBus.Introspectable"><method name="Introspect"><arg name="xml" type="s" direction="out"/>
</method></interface><interface name="org.freedesktop.DBus.ObjectManager"><method name="GetManagedObjects"><arg name="objects" type="a{oa{sa{sv}}}" direction="out"/>
</method><signal name="InterfacesAdded"><arg name="object" type="o"/>
<arg name="interfaces" type="a{sa{sv}}"/>
</signal>
<signal name="InterfacesRemoved"><arg name="object" type="o"/>
<arg name="interfaces" type="as"/>
</signal>
</interface><node name="org"/></node>

从这个网站(http://techbase.kde.org/Development/Tutorials/D-Bus/CustomTypes)我了解到,我需要在XML中添加注释才能使工具正常工作。
以下是我目前的进展:
a{oa{sa{sv}}}

https://alteeve.ca/w/List_of_DBus_data_types
o == A UTF-8 string whose value is a valid DBus object path.

array { object_path array { string array { string variant } } }

<arg name="customdata" type="a{sv}" direction="in" />
QVariantMap in the arguments (type "a{sv}")
QMap<QString, QVariant>

然而,我不确定a{oa{sa{sv}}}的注释应该是什么,有人能帮助我理解吗?谢谢!

2个回答

11

openSUSE imagewriter是一个GPL许可的项目,其中包含如何执行此操作的示例。
(相关文件:udisks2_interface.*


a{sv}是字符串:变体对的字典。
QVariantMap适合此签名。

a{sa{sv}}是字符串:a{sv}对的字典。
QMap<QString, QVariantMap>适合此签名。

a{oa{sa{sv}}}是对象路径:a{sa{sv}}对的字典。
QMap<QDBusObjectPath, QMap<QString, QVariantMap>>适合此签名。

我们应该在头文件中使用一些typedef来隐藏这些尖括号:

typedef QMap<QString, QVariantMap> InterfaceList;
typedef QMap<QDBusObjectPath, InterfaceList> ManagedObjectList;

接着在同一个头文件中声明它们的 QMetaTypes:

Q_DECLARE_METATYPE(InterfaceList)
Q_DECLARE_METATYPE(ManagedObjectList)

然后在运行时将它们注册到Qt元类型系统中:

qDBusRegisterMetaType<InterfaceList>();
qDBusRegisterMetaType<ManagedObjectList>();

然后,我们可以对XML进行注释:

<method name="GetManagedObjects">
  <arg type="a{oa{sa{sv}}}" name="objects" direction="out" />
  <annotation name="org.qtproject.QtDBus.QtTypeName.Out0" value="ManagedObjectList"/>
</method>
<signal name="InterfacesAdded">
  <arg type="o" name="object"/>
  <arg type="a{sa{sv}}" name="interfaces" />
  <annotation name="org.qtproject.QtDBus.QtTypeName.In1" value="InterfaceList"/>
</signal>

给定的注释语法对我不起作用(Qt 5.9.2)。我得到了与问题作者一样的qdbusxml2cpp错误。 - Artem Pisarenko
1
需要修改XML语法:每个annotation标签应该从arg标签中取出,并单独放在它后面。 例子: <arg type="..." name="..."/><annotation name="..." value="..."/> 而不是 <arg type="..." name="..."><annotation name="..." value="..."/></arg> - Artem Pisarenko
@ArtemPisarenko 您的编辑看起来很不错,我已经批准了,谢谢。 - Oktalist

1

我也遇到了新版本(Qt5.12)的相同问题。我不想修改生成的代码,于是发现如何正确生成此代码。
qdbusxml2cpp不确定如何处理字典签名。我的解决办法是直接使用Qt类型添加注释,例如:
"a{oa{sa{sv}}}" -> QMap<QDBusObjectPath, QMap<QString, QVariantMap> >

<method name="GetManagedObjects">
  <arg type="a{oa{sa{sv}}}" name="object_paths_interfaces_and_properties" direction="out"/>
  <annotation name="org.qtproject.QtDBus.QtTypeName.Out0" value="QMap&lt;QDBusObjectPath,QMap&lt;QString,QVariantMap&gt;&gt;"/>
</method>

使用 &lt 代替 <,使用 &gt 代替 >。这样生成的代码就是正确的。

希望这能在8年后某天帮助到某个人...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接