Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Connect to DBus signal - Correct syntax
Forum Updated to NodeBB v4.3 + New Features

Connect to DBus signal - Correct syntax

Scheduled Pinned Locked Moved Solved Qt for Python
4 Posts 2 Posters 674 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Drexel
    wrote on 11 Dec 2023, 20:09 last edited by
    #1

    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 ochangeslot

    Thank you in advance for any help!

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Drexel
      wrote on 18 Dec 2023, 08:01 last edited by
      #2

      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())
      
      S 1 Reply Last reply 18 Dec 2023, 08:46
      1
      • D Drexel has marked this topic as solved on 18 Dec 2023, 08:10
      • D Drexel
        18 Dec 2023, 08:01

        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())
        
        S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 18 Dec 2023, 08:46 last edited by
        #3

        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 ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • D Offline
          D Offline
          Drexel
          wrote on 18 Dec 2023, 13:19 last edited by
          #4

          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-hangs

          If 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 Reply Last reply
          0

          1/4

          11 Dec 2023, 20:09

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved