我正在尝试使用 PyQt4(特别是 QtDBus)在 DBus 上运行一些基本代码。我正在使用 PyQt4 的 Python3 版本。我已经获得了想要在 Qt (c++) 上运行的代码,但我想仅使用 Python 来运行类似的代码。我想在 DBus 上公开方法、信号/槽和属性,以供其他 Python 代码调用。
在 Qt 中,您使用 Q_CLASSINFO 宏/函数来进行 DBus 自省。虽然我已经引入了 Q_CLASSINFO 方法,但我无法让它产生相同类型的功能。据我所知,关于 Q_CLASSINFO 方法的文档为零,所以我不确定是否还有其他方法。使用 D-Feet,我可以清楚地看到没有自动公开任何方法,所以我有点卡住了。
这是我到目前为止所拥有的。
from PyQt4 import QtDBus
from PyQt4.QtCore import QCoreApplication, QObject, Q_CLASSINFO, pyqtSlot, pyqtProperty
from PyQt4.QtDBus import QDBusConnection, QDBusAbstractAdaptor
SERVICE = 'com.home.dbus'
class MyServer(QObject):
def __init__(self):
QObject.__init__(self)
self.__dbusAdaptor = ServerAdaptor(self)
def close(self):
pass
def echo(self, value):
echoed = 'Received {0}'.format(value)
return echoed
def name(self):
return 'myname'
def dbus_adaptor(self):
return self.__dbusAdaptor
class ServerAdaptor(QDBusAbstractAdaptor):
""" This provides the DBus adaptor to the outside world"""
def __init__(self, parent):
super().__init__(parent)
self.__parent = parent
Q_CLASSINFO("D-Bus Introspection",
" <interface name=\"com.home.dbus\">\n"
" <method name=\"name\">\n"
" <arg direction=\"out\" type=\"s\" name=\"name\"/>\n"
" </method>\n"
" <method name=\"echo\">\n"
" <arg direction=\"in\" type=\"s\" name=\"phrase\"/>\n"
" <arg directory=\"out\" type=\"s\" name=\"echoed\"/>\n"
" </method>\n"
" </interface>\n")
def close(self):
parent.close()
def echo(self, value):
return parent.echo(value)
def name(self):
return parent.name
def start():
app = QCoreApplication([])
if QDBusConnection.sessionBus().isConnected() == False:
print('Cannot connect to D-Bus session bus')
return
print('Starting')
server = MyServer()
if not QDBusConnection.sessionBus().registerService(SERVICE):
print('Unable to register service name')
return
if not QDBusConnection.sessionBus().registerObject('/mydbus', server.dbus_adaptor):
print('Unable to register object at service path')
return
app.exec();
print('Exited')
if __name__ == '__main__':
start()
虽然我真的很喜欢在 C++ 中使用 QtDBus,因为我想如何构建我的这个大型项目,但我确实需要通过 DBus 访问的对象用 Python3 编写。
您的程序存在几个问题。我建议看看remotecontrolledcar
and pingpong
来自最新 PyQt 源的示例,它们非常有帮助。主要注意事项是:
- 你应该通过一个
MyServer
实例(不是ServerAdaptor
) to registerObject()
- Add
pyqtSlot()
您希望通过 D-Bus 公开的函数的装饰器
- Call
Q_CLASSINFO()
在适配器类的顶部,而不是在其内部__init__()
功能
- 还使用设置“D-Bus Interface”
Q_CLASSINFO()
- 您的内省 XML 包含拼写错误(“目录”而不是“方向”)
这是一个适合我的精简示例(Python 3.2.3/Qt 4.8.2/PyQt 4.9.4):
from PyQt4 import QtDBus
from PyQt4.QtCore import (QCoreApplication, QObject, Q_CLASSINFO, pyqtSlot,
pyqtProperty)
from PyQt4.QtDBus import QDBusConnection, QDBusAbstractAdaptor
class MyServer(QObject):
def __init__(self):
QObject.__init__(self)
self.__dbusAdaptor = ServerAdaptor(self)
self.__name = 'myname'
def echo(self, value):
return'Received: {0}'.format(value)
@property
def name(self):
return self.__name
@name.setter
def name(self, value):
self.__name = value
class ServerAdaptor(QDBusAbstractAdaptor):
""" This provides the DBus adaptor to the outside world"""
Q_CLASSINFO("D-Bus Interface", "com.home.dbus")
Q_CLASSINFO("D-Bus Introspection",
' <interface name="com.home.dbus">\n'
' <property name="name" type="s" access="readwrite"/>\n'
' <method name="echo">\n'
' <arg direction="in" type="s" name="phrase"/>\n'
' <arg direction="out" type="s" name="echoed"/>\n'
' </method>\n'
' </interface>\n')
def __init__(self, parent):
super().__init__(parent)
@pyqtSlot(str, result=str)
def echo(self, phrase):
return self.parent().echo(phrase)
@pyqtProperty(str)
def name(self):
return self.parent().name
@name.setter
def name(self, value):
self.parent().name = value
def start():
app = QCoreApplication([])
bus = QDBusConnection.sessionBus()
server = MyServer()
bus.registerObject('/mydbus', server)
bus.registerService('com.home.dbus')
app.exec()
if __name__ == '__main__':
start()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)