使用qdbusxml2cpp生成适配器为何缺少Introspect方法?
为什么qdbusxml2cpp生成的适配器类缺少Introspect方法
核心原因
org.freedesktop.DBus.Introspectable是D-Bus的标准内置接口,Qt的DBus框架已经在底层自动实现了这个接口的所有功能,不需要qdbusxml2cpp从你的自定义XML中生成对应的方法代码。
也就是说,虽然生成的适配器类里看不到Introspect方法的声明,但你的DBus对象在运行时天然支持这个方法——Qt会自动处理该接口的调用请求,返回你的对象的自省XML数据。
为什么XML里没定义就不会生成
qdbusxml2cpp的作用是根据你提供的XML中自定义的接口和方法生成代码,而D-Bus的三个标准内置接口(Introspectable、Properties、Peer)属于框架级别的默认实现,不在工具的代码生成范围内。
验证方法
你可以用qdbus命令行工具直接调用该方法,确认它确实存在:
qdbus com.company.product /com/company/product/interface org.freedesktop.DBus.Introspectable.Introspect
执行后会返回你自定义接口的完整自省XML,证明方法正常工作。
可选:显式添加接口到XML(不推荐)
如果一定要让生成的类里出现Introspect方法的声明,可以手动在你的XML中添加该接口的定义:
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd"> <node name="/com/company/product/interface"> <interface name="org.freedesktop.DBus.Introspectable"> <method name="Introspect"> <arg type="s" direction="out"/> </method> </interface> <interface name="com.company.product.interface"> <method name="sendCommand"> <arg type="s" name="device" direction="in"/> <arg type="s" name="command" direction="in"/> <arg type="b" name="success" direction="out"/> </method> <method name="sendCommandEx"> <arg type="s" name="device" direction="in"/> <arg type="s" name="command" direction="in"/> <arg type="b" name="success" direction="out"/> <arg type="s" name="result" direction="out"/> </method> </interface> </node>
但这么做完全没必要,因为Qt已经自动实现了该方法,你不需要编写任何额外代码。
内容的提问来源于stack exchange,提问作者George Tuzikov
相关产品推荐
相关产品推荐

