我在使用QtDbus in PyQt5时遇到了问题。我想做的是发现一个特定的Avahi-/Zeroconf服务类型"_mumble._tcp“。启动/停止服务时,当启动脚本时,我可以看到dbus-monitor --system中发出的信号:
signal sender=:1.138 -> dest=:1.273 serial=1413 path=/Client37/ServiceBrowser1; interface=org.freedesktop.Avahi.ServiceBrowser; member=ItemNew
int32 5
int32 0
string "hetzner"
string "_mumble._tcp"
string "local"
uint32 4这是我的代码样本。如果我观察其他被广播的信号,它确实可以工作,但它不适用于使用创建的ServiceBrowser通过Avahi发现服务,也就是说,它不会在ServiceBrowser发出的"ItemNew“上触发。
#!/usr/bin/python3
import sys
from PyQt5.QtCore import QObject, QVariant, pyqtSlot
from PyQt5.QtWidgets import QApplication
from PyQt5.QtDBus import QDBusConnection, QDBusInterface
class ServiceDiscoverer(QObject):
def __init__(self):
super(ServiceDiscoverer, self).__init__()
bus = QDBusConnection.systemBus()
server = QDBusInterface(
'org.freedesktop.Avahi',
'/',
'org.freedesktop.Avahi.Server',
bus
)
flags = QVariant(0)
flags.convert(QVariant.UInt)
browser_path = server.call(
'ServiceBrowserNew',
-1, # Interface
-1, # Protocol -1 = both, 0 = ipv4, 1 = ipv6
'_mumble._tcp',
'', # Domain, default to .local
flags
)
bus.connect(
'org.freedesktop.Avahi',
browser_path.arguments()[0],
'org.freedesktop.Avahi.ServiceBrowser',
'ItemNew',
self.onServerAdd
)
@pyqtSlot(int, int, str, str, str, int)
def onServerAdd(self, interface, protocol, key, _type, domain, flags):
print("Avahi peer added:")
print(" + Interface: %s" % interface)
print(" + Protocol: %s" % protocol)
print(" + Key: %s" % key)
print(" + Type: %s" % _type)
print(" + Domain: %s" % domain)
if __name__ == '__main__':
app = QApplication(sys.argv)
discoverer = ServiceDiscoverer()
sys.exit(app.exec_())我想我叫bus.connect()的方式有问题。有人对如何捕捉信号有建议吗?
发布于 2015-07-11 09:17:17
Qt找不到你的位置。您需要重新修饰您的插槽--您接收到的(通过DBUS)不是带有6个参数的函数调用,而是一个单一的QDBusMessage。试试这个:
...
@pyqtSlot(QDBusMessage)
def onServerAdd (self, msg):
print("Avahi peer added:")
print(" + Interface: %s" % msg.arguments()[0])
print(" + Protocol: %s" % msg.arguments()[1])
print(" + Key: %s" % msg.arguments()[2])
print(" + Type: %s" % msg.arguments()[3])
print(" + Domain: %s" % msg.arguments()[4])
...https://stackoverflow.com/questions/28657417
复制相似问题