Qt for Python DBusIntegration

From Qt Wiki
Jump to: navigation, search


To get PySide2 and DBus working together you can use the glib mainloop integration already done in dbus-python.

The examples below show how to export Qt objects to Python and emit a D-Bus signal when a Qt signal is emitted. The code comments explain what you need to know about PySide2 and D-Bus.

Also refer to the dbus-python tutorial.

D-Bus Client

 1 #!/usr/bin/env python
 2 # -'''- coding: utf-8 -'''-
 3 
 4 # DBUS Client using PySide2 integration
 5 
 6 import sys
 7 from traceback import print_exc
 8 
 9 # import python dbus module
10 import dbus
11 # import python dbus GLib mainloop support
12 import dbus.mainloop.glib
13 # import QtCore
14 from PySide2.QtCore import *
15 
16 # signal handler
17 def button_clicked():
18     print("button clicked")
19 
20 # main function
21 if __name__ == '__main__':
22     # Enable glib main loop support
23     dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
24     # Get the session bus
25     bus = dbus.SessionBus()
26     
27     try:
28         # Get the remote object
29         remote_object = bus.get_object("com.example.SampleService",
30         "/DBusWidget")
31         # Get the remote interface for the remote object
32         iface = dbus.Interface(remote_object, "com.example.SampleWidget")
33     except dbus.DBusException:
34         print_exc()
35         sys.exit(1)
36     
37     # Start the application
38     app = QCoreApplication([])
39     
40     # Call some methods of the remote interface
41     iface.show()
42     iface.setText("Emit signal")
43     # connect the DBus signal clicked to the function button_clicked
44     iface.connect_to_signal("clicked", button_clicked)
45     iface.connect_to_signal("lastWindowClosed", app.quit)
46     
47     # enter in the main loop
48     app.exec_()

D-Bus Server

 1 #!/usr/bin/env python
 2 #-'''- coding: utf-8 -'''-
 3 
 4 # DBUS Server Example of use PySide2 with dbus-python library
 5 
 6 import dbus
 7 import dbus.service
 8 import dbus.mainloop.glib
 9 import random
10 
11 from PySide2.QtCore import *
12 from PySide2.QtWidgets import QPushButton, QApplication
13 
14 # The adaptor, MUST inherit dbus.service.Object
15 class DBusWidget(dbus.service.Object):
16     def __init__(self, name, session):
17         # export this object to dbus
18         dbus.service.Object.__init__(self, name, session)
19 
20         # create a simple widget
21         self.widget = QPushButton()
22         self.widget.resize(200, 50)
23 
24         # To export a Qt signal as a DBus-signal, you need to connect it to
25         # a method in this class.
26         # The method MUST have the signal annotation, so dbus-python will
27         # export it as a dbus-signal
28         QObject.connect(self.widget, SIGNAL ("clicked()"), self.clicked)
29         QObject.connect(QApplication.instance(), SIGNAL ("lastWindowClosed()"),
30             self.lastWindowClosed)
31 
32     # You can export methods to dbus like you do in python-dbus.
33     @dbus.service.method("com.example.SampleWidget", in_signature='', out_signature='')
34     def show(self):
35         self.widget.show()
36     
37     # Another method… now with a parameter
38     @dbus.service.method("com.example.SampleWidget", in_signature='s', out_signature='')
39     def setText(self, value):
40         self.widget.setText(value)
41     
42     # Another one…
43     @dbus.service.method("com.example.SampleWidget", in_signature='', out_signature='')
44     def exit(self):
45         qApp().quit()
46     
47     # A signal that will be exported to dbus
48     @dbus.service.signal("com.example.SampleWidget", signature='')
49     def clicked(self):
50         pass
51     
52     # Another signal that will be exported to dbus
53     @dbus.service.signal("com.example.SampleWidget", signature='')
54     def lastWindowClosed(self):
55         pass
56 
57 if __name__ == "__main__":
58     print("holi")
59     app = QApplication([])
60     # Use qt/glib mainloop integration to get dbus mainloop working
61     dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
62     
63     session_bus = dbus.SessionBus()
64     print(session_bus)
65     # Export the service
66     name = dbus.service.BusName("com.example.SampleService", session_bus)
67     # Export the object
68     widget = DBusWidget(session_bus, '/DBusWidget')
69     
70     print("Running

Running the examples

Copy the client code to a file called example-client.py and the server to a file called example-server.py and type:

python example-server.py &
python example-client.py

A small window should appear on screen. Click on the button to emit a Qt signal. The signal will be converted to a D-Bus signal that will be caught by our D-Bus client.