PySide D-Bus Integration

To get PySide and DBus working toghether you can use the glib mainloop integration already done in pydbus.

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 PySide and D-Bus.

Also refer to the dbus-python tutorial [dbus.freedesktop.org].

D-Bus Client

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

D-Bus Server

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # DBUS Server Example of use PySide with PyDBus library
  5.  
  6. import dbus
  7. import dbus.service
  8. import dbus.mainloop.glib
  9. import random
  10.  
  11. from PySide.QtCore import *
  12. from PySide.QtGui 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 a method in this class.
  25.         # The method MUST have the signal annotation, so python-dbus will export it as a dbus-signal
  26.         QObject.connect(self.widget, SIGNAL("clicked()"), self.clicked)
  27.         QObject.connect(QApplication.instance(), SIGNAL("lastWindowClosed()"), self.lastWindowClosed)
  28.  
  29.     # You can export methods to dbus like you do in python-dbus.
  30.     @dbus.service.method("com.example.SampleWidget", in_signature='', out_signature='')
  31.     def show(self):
  32.         self.widget.show()
  33.  
  34.     # Another method... now with a parameter
  35.     @dbus.service.method("com.example.SampleWidget", in_signature='s', out_signature='')
  36.     def setText(self, value):
  37.         self.widget.setText(value)
  38.  
  39.     # Another one...
  40.     @dbus.service.method("com.example.SampleWidget", in_signature='', out_signature='')
  41.     def exit(self):
  42.         qApp().quit()
  43.  
  44.     # A signal that will be exported to dbus
  45.     @dbus.service.signal("com.example.SampleWidget", signature='')
  46.     def clicked(self):
  47.         pass
  48.  
  49.     # Another signal that will be exported to dbus
  50.     @dbus.service.signal("com.example.SampleWidget", signature='')
  51.     def lastWindowClosed(self):
  52.         pass
  53.  
  54.  
  55. if __name__ == '__main__':
  56.     app = QApplication([])
  57.     # Use qt/glib mainloop integration to get dbus mainloop working
  58.     dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
  59.  
  60.     session_bus = dbus.SessionBus()
  61.     # Export the service
  62.     name = dbus.service.BusName("com.example.SampleService", session_bus)
  63.     # Export the object
  64.     widget = DBusWidget(session_bus, '/DBusWidget')
  65.  
  66.     print "Running example service."
  67.     app.exec_()

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:

  1. python example-server.py &
  2. 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.

Categories: