我正在尝试使用PyQt4在DBus上运行一些基本代码,特别是QtDBus。我正在使用Python3版本的PyQt4。我已经得到了我想在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编写。
答案 0 :(得分:5)
您的计划存在一些问题。我建议您查看最新PyQt源代码中的remotecontrolledcar
和pingpong
示例,它们非常有用。需要注意的要点是:
MyServer
个实例(不是ServerAdaptor
)传递给registerObject()
pyqtSlot()
装饰器添加到您希望通过D-Bus公开的功能Q_CLASSINFO()
,而不是__init__()
函数Q_CLASSINFO()
这是一个适用于我的精简示例(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()