Python Forum
PySide6 - Connect to DBus signal - Correct syntax
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PySide6 - Connect to DBus signal - Correct syntax
#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!
Reply
#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())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PySide6 Copy and Past from clipboard to QTableWedget zinho 6 1,292 Dec-07-2023, 10:10 PM
Last Post: zinho
Bug [PyQt] Dinamically adding widgets to layout [PySide6] carecavoador 2 1,476 Jun-19-2023, 12:57 PM
Last Post: carecavoador
  PySide6 QFontDialog - bug or just me? PatM 1 1,118 Jan-22-2023, 01:29 PM
Last Post: Axel_Erfurt
  Pyside6 Larz60+ 7 3,003 Nov-28-2022, 07:25 PM
Last Post: Larz60+
  [PySide6] Load ui with UiLoader catlessness 6 8,871 Nov-24-2021, 02:17 PM
Last Post: catlessness

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020