Qt for Python DBusIntegration: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
=PySide D-Bus Integration= | [[Category:LanguageBindings::PySide]] | ||
= PySide D-Bus Integration = | |||
To get PySide and DBus working toghether you can use the glib mainloop integration already done in pydbus. | To get PySide and DBus working toghether you can use the glib mainloop integration already done in pydbus. | ||
Line 5: | Line 7: | ||
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. | 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 | Also refer to the "dbus-python tutorial":http://dbus.freedesktop.org/doc/dbus-python/doc/tutorial.html. | ||
== D-Bus Client == | |||
<code><br />#!/usr/bin/env python<br /># <s>'''- coding: utf-8 <s>'''- | |||
<br /># DBUS Client using PySide integration | |||
<br />import sys<br />from traceback import print_exc | |||
<br /># import python dbus module<br />import dbus<br /># import python dbus GLib mainloop support<br />import dbus.mainloop.glib<br /># import QtCore<br />from PySide.QtCore import''' | |||
<br /># signal handler<br />def button_clicked():<br /> print "button clicked&quot; | |||
<br /># main function<br />if ''name'' == '''main''': | |||
<br /> # Enable glib main loop support<br /> dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)<br /> # Get the session bus<br /> bus = dbus.SessionBus() | |||
<br /> try:<br /> # Get the remote object<br /> remote_object = bus.get_object("com.example.SampleService&quot;,<br /> "/DBusWidget&quot;)<br /> # Get the remote interface for the remote object<br /> iface = dbus.Interface(remote_object, "com.example.SampleWidget&quot;)<br /> except dbus.DBusException:<br /> print_exc()<br /> sys.exit(1) | |||
<br /> # Start the application<br /> app = QCoreApplication([]) | |||
<br /> # Call some methods of the remote interface<br /> iface.show()<br /> iface.setText("Emit signal&quot;)<br /> # connect the DBus signal clicked to the function button_clicked<br /> iface.connect_to_signal("clicked&quot;, button_clicked)<br /> iface.connect_to_signal("lastWindowClosed&quot;, app.quit) | |||
<br /> # enter in the main loop<br /> app.exec_()<br /></code> | |||
<br />h3. D-Bus Server | |||
<br /><code><br />#!/usr/bin/env python<br />#</s>'''</s> coding: utf-8 -'''- | |||
<br /># DBUS Server Example of use PySide with PyDBus library | |||
<br />import dbus<br />import dbus.service<br />import dbus.mainloop.glib<br />import random | |||
<br />from PySide.QtCore import'''<br />from PySide.QtGui import QPushButton, QApplication | |||
# The adaptor, MUST inherit dbus.service.Object<br />class DBusWidget(dbus.service.Object):<br /> def ''init''(self, name, session):<br /> # export this object to dbus<br /> dbus.service.Object.''init''(self, name, session) | |||
# create a simple widget<br /> self.widget = QPushButton()<br /> self.widget.resize(200, 50) | |||
# To export a Qt signal as a DBus-signal, you need to connect it to a method in this class.<br /> # The method MUST have the signal annotation, so python-dbus will export it as a dbus-signal<br /> QObject.connect(self.widget, SIGNAL (&quot;clicked()"), self.clicked)<br /> QObject.connect(QApplication.instance(), SIGNAL (&quot;lastWindowClosed()"), self.lastWindowClosed) | |||
# You can export methods to dbus like you do in python-dbus.<br /> &#64;dbus.service.method("com.example.SampleWidget&quot;, in_signature='', out_signature='')<br /> def show(self):<br /> self.widget.show() | |||
== | # Another method… now with a parameter<br /> &#64;dbus.service.method("com.example.SampleWidget&quot;, in_signature='s', out_signature='')<br /> def setText(self, value):<br /> self.widget.setText(value) | ||
== | # Another one…<br /> &#64;dbus.service.method("com.example.SampleWidget&quot;, in_signature='', out_signature='')<br /> def exit(self):<br /> qApp().quit() | ||
===Running the examples=== | # A signal that will be exported to dbus<br /> &#64;dbus.service.signal("com.example.SampleWidget&quot;, signature='')<br /> def clicked(self):<br /> pass | ||
# Another signal that will be exported to dbus<br /> &#64;dbus.service.signal("com.example.SampleWidget&quot;, signature='')<br /> def lastWindowClosed(self):<br /> pass | |||
if ''name'' == '''main''':<br /> app = QApplication([])<br /> # Use qt/glib mainloop integration to get dbus mainloop working<br /> dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) | |||
session_bus = dbus.SessionBus()<br /> # Export the service<br /> name = dbus.service.BusName("com.example.SampleService&quot;, session_bus)<br /> # Export the object<br /> widget = DBusWidget(session_bus, '/DBusWidget') | |||
print "Running example service."<br /> app.exec_()<br /></code> | |||
=== 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: | Copy the client code to a file called example-client.py and the server to a file called example-server.py and type: | ||
<code><br />python example-server.py &<br />python example-client.py<br /></code> | |||
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. | 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. | ||
Revision as of 09:37, 24 February 2015
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":http://dbus.freedesktop.org/doc/dbus-python/doc/tutorial.html.
D-Bus Client
<br />#!/usr/bin/env python<br /># <s>'''- coding: utf-8 <s>'''-
<br /># DBUS Client using PySide integration
<br />import sys<br />from traceback import print_exc
<br /># import python dbus module<br />import dbus<br /># import python dbus GLib mainloop support<br />import dbus.mainloop.glib<br /># import QtCore<br />from PySide.QtCore import'''
<br /># signal handler<br />def button_clicked():<br /> print "button clicked&quot;
<br /># main function<br />if ''name'' == '''main''':
<br /> # Enable glib main loop support<br /> dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)<br /> # Get the session bus<br /> bus = dbus.SessionBus()
<br /> try:<br /> # Get the remote object<br /> remote_object = bus.get_object("com.example.SampleService&quot;,<br /> "/DBusWidget&quot;)<br /> # Get the remote interface for the remote object<br /> iface = dbus.Interface(remote_object, "com.example.SampleWidget&quot;)<br /> except dbus.DBusException:<br /> print_exc()<br /> sys.exit(1)
<br /> # Start the application<br /> app = QCoreApplication([])
<br /> # Call some methods of the remote interface<br /> iface.show()<br /> iface.setText("Emit signal&quot;)<br /> # connect the DBus signal clicked to the function button_clicked<br /> iface.connect_to_signal("clicked&quot;, button_clicked)<br /> iface.connect_to_signal("lastWindowClosed&quot;, app.quit)
<br /> # enter in the main loop<br /> app.exec_()<br />
h3. D-Bus Server
<br />#!/usr/bin/env python<br />#</s>'''</s> coding: utf-8 -'''-
<br /># DBUS Server Example of use PySide with PyDBus library
<br />import dbus<br />import dbus.service<br />import dbus.mainloop.glib<br />import random
<br />from PySide.QtCore import'''<br />from PySide.QtGui import QPushButton, QApplication
# The adaptor, MUST inherit dbus.service.Object<br />class DBusWidget(dbus.service.Object):<br /> def ''init''(self, name, session):<br /> # export this object to dbus<br /> dbus.service.Object.''init''(self, name, session)
# create a simple widget<br /> self.widget = QPushButton()<br /> self.widget.resize(200, 50)
# To export a Qt signal as a DBus-signal, you need to connect it to a method in this class.<br /> # The method MUST have the signal annotation, so python-dbus will export it as a dbus-signal<br /> QObject.connect(self.widget, SIGNAL (&quot;clicked()"), self.clicked)<br /> QObject.connect(QApplication.instance(), SIGNAL (&quot;lastWindowClosed()"), self.lastWindowClosed)
# You can export methods to dbus like you do in python-dbus.<br /> &#64;dbus.service.method("com.example.SampleWidget&quot;, in_signature='', out_signature='')<br /> def show(self):<br /> self.widget.show()
# Another method… now with a parameter<br /> &#64;dbus.service.method("com.example.SampleWidget&quot;, in_signature='s', out_signature='')<br /> def setText(self, value):<br /> self.widget.setText(value)
# Another one…<br /> &#64;dbus.service.method("com.example.SampleWidget&quot;, in_signature='', out_signature='')<br /> def exit(self):<br /> qApp().quit()
# A signal that will be exported to dbus<br /> &#64;dbus.service.signal("com.example.SampleWidget&quot;, signature='')<br /> def clicked(self):<br /> pass
# Another signal that will be exported to dbus<br /> &#64;dbus.service.signal("com.example.SampleWidget&quot;, signature='')<br /> def lastWindowClosed(self):<br /> pass
if ''name'' == '''main''':<br /> app = QApplication([])<br /> # Use qt/glib mainloop integration to get dbus mainloop working<br /> dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
session_bus = dbus.SessionBus()<br /> # Export the service<br /> name = dbus.service.BusName("com.example.SampleService&quot;, session_bus)<br /> # Export the object<br /> widget = DBusWidget(session_bus, '/DBusWidget')
print "Running example service."<br /> app.exec_()<br />
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:
<br />python example-server.py &<br />python example-client.py<br />
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.