Search code examples
pythonpysidedbuspyside6

Connect to DBus signal - Correct syntax - PySide6


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:

from PySide6 import QtDBus
from PySide6.QtCore import Q
from PySide6.QtWidgets import QMainWindow

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 ochangeslot

Please consider this is a PySide6 issue and not a PyQt5 issue, the signature of the call is slightly different and my code doesn't hang like in similar topics on stackoverflow.

Thank you in advance for any help!


Solution

  • This es the full solution to my initial post, I got this running with the QT/PySide support and they also acknowledged the hangig bug and a Python crash:

    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())