Connect to DBus signal - Correct syntax
-
wrote on 11 Dec 2023, 20:09 last edited by
Hello,
can you help me with the correct syntax to connect to a DBus signal?
This is one of my many tries which at least runs and matches the signature in the docs:
class MainWindow(QMainWindow): __slots__ = ["__mainwidget"] __mainwidget:QWidget def __init__ (self, *args, **kwargs): super().__init__(*args, **kwargs) service = 'org.freedesktop.DBus' path = '/org/freedesktop/DBus' iface = 'org.freedesktop.DBus' conn = QtDBus.QDBusConnection.systemBus() conn.connect(service, path, iface, "NameOwnerChanged", self, "nochangeslot") #smp = QtDBus.QDBusInterface(service, path, iface, connection=QtDBus.QDBusConnection.systemBus() def nochangeslot(self, arg:object) -> None: print(arg) pass
But it doesn't work and looks weird with the slot as string...
On the output I see:
qt.dbus.integration: Could not connect "org.freedesktop.DBus" to ochangeslotThank you in advance for any help!
-
wrote on 18 Dec 2023, 08:01 last edited by
With help of Qt support, I managed this to work, the trick was to use the SLOT macro:
import sys from PySide6.QtWidgets import QMainWindow from PySide6 import QtDBus, QtCore from PySide6.QtCore import QLibraryInfo, qVersion, Slot from PySide6.QtWidgets import QApplication, QMainWindow class MainWindow(QMainWindow): def __init__ (self, *args, **kwargs): super().__init__(*args, **kwargs) service = "org.freedesktop.DBus" path = "/org/freedesktop/DBus" iface = "org.freedesktop.DBus" conn = QtDBus.QDBusConnection.systemBus() #without this, the conn.connect call hangs, seems to be a bug, is already reported and fixed. conn.registerObject('/', self) conn.connect(service, path, iface, "NameOwnerChanged", self, QtCore.SLOT("nameownerchanged(QString, QString, QString)")) pass @Slot(str, str, str) def nameownerchanged(self, arg1:str, arg2:str, arg3:str) -> None: print(arg1) print(arg2) print(arg3) pass if __name__ == '__main__': print('Python {}.{}.{} {}'.format(sys.version_info[0], sys.version_info[1], sys.version_info[2], sys.platform)) print(QLibraryInfo.build()) app = QApplication(sys.argv) window = MainWindow() window.setWindowTitle(qVersion()) window.resize(800, 480) window.show() sys.exit(app.exec())
-
-
With help of Qt support, I managed this to work, the trick was to use the SLOT macro:
import sys from PySide6.QtWidgets import QMainWindow from PySide6 import QtDBus, QtCore from PySide6.QtCore import QLibraryInfo, qVersion, Slot from PySide6.QtWidgets import QApplication, QMainWindow class MainWindow(QMainWindow): def __init__ (self, *args, **kwargs): super().__init__(*args, **kwargs) service = "org.freedesktop.DBus" path = "/org/freedesktop/DBus" iface = "org.freedesktop.DBus" conn = QtDBus.QDBusConnection.systemBus() #without this, the conn.connect call hangs, seems to be a bug, is already reported and fixed. conn.registerObject('/', self) conn.connect(service, path, iface, "NameOwnerChanged", self, QtCore.SLOT("nameownerchanged(QString, QString, QString)")) pass @Slot(str, str, str) def nameownerchanged(self, arg1:str, arg2:str, arg3:str) -> None: print(arg1) print(arg2) print(arg3) pass if __name__ == '__main__': print('Python {}.{}.{} {}'.format(sys.version_info[0], sys.version_info[1], sys.version_info[2], sys.platform)) print(QLibraryInfo.build()) app = QApplication(sys.argv) window = MainWindow() window.setWindowTitle(qVersion()) window.resize(800, 480) window.show() sys.exit(app.exec())
Hi and welcome to devnet,
Thanks for the follow up and details !
Do you also have a link handy for the object registration issue you mention in your code ?
-
wrote on 18 Dec 2023, 13:19 last edited by
Here is the complete issue: https://bugreports.qt.io/browse/PYSIDE-2547
I got this solution from here: https://stackoverflow.com/questions/38142809/pyqt-5-6-connecting-to-a-dbus-signal-hangsIf you have an idea how to get the PropertiesChanged Signal to work, I would be happy, too, otherwise I will switch to GTK I guess... Or maybe I open a separate post for this, but as no one could help on the simple case, I have little hope for the more complex case...
1/4