aboutsummaryrefslogtreecommitdiffstats
path: root/doc/codesnippets/examples/dbus/example-client.py
blob: 605d4b452bf78b30f7972cc7855a918ffe4282b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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_()