Add dbus example do docs.
Reviewer: Bruno Araújo <bruno.araujo@openbossa.org> Reviewer: Renato Araújo <renato.araujo@openbossa.org>
This commit is contained in:
parent
e7f6729d5b
commit
59d0798159
3 changed files with 120 additions and 3 deletions
49
doc/codesnippets/examples/dbus/example-client.py
Executable file
49
doc/codesnippets/examples/dbus/example-client.py
Executable file
|
|
@ -0,0 +1,49 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# DBUS Client using PySide integration
|
||||||
|
|
||||||
|
import sys
|
||||||
|
from traceback import print_exc
|
||||||
|
|
||||||
|
# import python dbus module
|
||||||
|
import dbus
|
||||||
|
# import python dbus GLib mainloop support
|
||||||
|
import dbus.mainloop.glib
|
||||||
|
# import QtCore
|
||||||
|
from PySide.QtCore import *
|
||||||
|
|
||||||
|
# signal handler
|
||||||
|
def button_clicked():
|
||||||
|
print "button clicked"
|
||||||
|
|
||||||
|
# main function
|
||||||
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
# Enable glib main loop support
|
||||||
|
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
|
||||||
|
# Get the session bus
|
||||||
|
bus = dbus.SessionBus()
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Get the remote object
|
||||||
|
remote_object = bus.get_object("com.example.SampleService",
|
||||||
|
"/DBusWidget")
|
||||||
|
# Get the remote interface for the remote object
|
||||||
|
iface = dbus.Interface(remote_object, "com.example.SampleWidget")
|
||||||
|
except dbus.DBusException:
|
||||||
|
print_exc()
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
# Start the application
|
||||||
|
app = QCoreApplication([])
|
||||||
|
|
||||||
|
# Call some methods of the remote interface
|
||||||
|
iface.show()
|
||||||
|
iface.setText("Emit signal")
|
||||||
|
# connect the DBus signal clicked to the function button_clicked
|
||||||
|
iface.connect_to_signal("clicked", button_clicked)
|
||||||
|
iface.connect_to_signal("lastWindowClosed", app.quit)
|
||||||
|
|
||||||
|
# enter in the main loop
|
||||||
|
app.exec_()
|
||||||
68
doc/codesnippets/examples/dbus/example-server.py
Executable file
68
doc/codesnippets/examples/dbus/example-server.py
Executable file
|
|
@ -0,0 +1,68 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# DBUS Server Example of use PySide with PyDBus library
|
||||||
|
|
||||||
|
import dbus
|
||||||
|
import dbus.service
|
||||||
|
import dbus.mainloop.glib
|
||||||
|
import random
|
||||||
|
|
||||||
|
from PySide.QtCore import *
|
||||||
|
from PySide.QtGui import QPushButton, QApplication
|
||||||
|
|
||||||
|
# The adaptor, MUST inherit dbus.service.Object
|
||||||
|
class DBusWidget(dbus.service.Object):
|
||||||
|
def __init__(self, name, session):
|
||||||
|
# export this object to dbus
|
||||||
|
dbus.service.Object.__init__(self, name, session)
|
||||||
|
|
||||||
|
# create a simple widget
|
||||||
|
self.widget = QPushButton()
|
||||||
|
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.
|
||||||
|
# The method MUST have the signal annotation, so python-dbus will export it as a dbus-signal
|
||||||
|
QObject.connect(self.widget, SIGNAL("clicked()"), self.clicked)
|
||||||
|
QObject.connect(QApplication.instance(), SIGNAL("lastWindowClosed()"), self.lastWindowClosed)
|
||||||
|
|
||||||
|
# You can export methods to dbus like you do in python-dbus.
|
||||||
|
@dbus.service.method("com.example.SampleWidget", in_signature='', out_signature='')
|
||||||
|
def show(self):
|
||||||
|
self.widget.show()
|
||||||
|
|
||||||
|
# Another method... now with a parameter
|
||||||
|
@dbus.service.method("com.example.SampleWidget", in_signature='s', out_signature='')
|
||||||
|
def setText(self, value):
|
||||||
|
self.widget.setText(value)
|
||||||
|
|
||||||
|
# Another one...
|
||||||
|
@dbus.service.method("com.example.SampleWidget", in_signature='', out_signature='')
|
||||||
|
def exit(self):
|
||||||
|
qApp().quit()
|
||||||
|
|
||||||
|
# A signal that will be exported to dbus
|
||||||
|
@dbus.service.signal("com.example.SampleWidget", signature='')
|
||||||
|
def clicked(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Another signal that will be exported to dbus
|
||||||
|
@dbus.service.signal("com.example.SampleWidget", signature='')
|
||||||
|
def lastWindowClosed(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
app = QApplication([])
|
||||||
|
# Use qt/glib mainloop integration to get dbus mainloop working
|
||||||
|
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
|
||||||
|
|
||||||
|
session_bus = dbus.SessionBus()
|
||||||
|
# Export the service
|
||||||
|
name = dbus.service.BusName("com.example.SampleService", session_bus)
|
||||||
|
# Export the object
|
||||||
|
widget = DBusWidget(session_bus, '/DBusWidget')
|
||||||
|
|
||||||
|
print "Running example service."
|
||||||
|
app.exec_()
|
||||||
|
|
||||||
|
|
@ -3,18 +3,18 @@ DBUS 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.
|
||||||
|
|
||||||
The example above show how to export Qt objects to python and emit an DBus signal when a Qtsignal is emited. The code comments explains what you need to know about PySide and dbus, any doubts, see the python-dbus help.
|
The example above show how to export Qt objects to python and emit an DBus signal when a Qt signal is emited. The code comments explains what you need to know about PySide and dbus, any doubts, see the python-dbus help.
|
||||||
|
|
||||||
DBUS Client
|
DBUS Client
|
||||||
-----------
|
-----------
|
||||||
|
|
||||||
.. literalinclude:: ../examples/dbus/example-client.py
|
.. literalinclude:: codesnippets/examples/dbus/example-client.py
|
||||||
|
|
||||||
|
|
||||||
DBUS Server
|
DBUS Server
|
||||||
-----------
|
-----------
|
||||||
|
|
||||||
.. literalinclude:: ../examples/dbus/example-server.py
|
.. literalinclude:: codesnippets/examples/dbus/example-server.py
|
||||||
|
|
||||||
|
|
||||||
Running the example
|
Running the example
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue