Qt for Python DBusIntegration: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Qt for Python]]


To get PySide2 and DBus working together you can use the glib mainloop integration already done in [https://pypi.org/project/dbus-python/ dbus-python].


[[Category:LanguageBindings::PySide]]
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.
 
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 [http://dbus.freedesktop.org/doc/dbus-python/doc/tutorial.html dbus-python tutorial].
Also refer to the [http://dbus.freedesktop.org/doc/dbus-python/doc/tutorial.html dbus-python tutorial].
Line 12: Line 10:
== D-Bus Client ==
== D-Bus Client ==


<code>
<syntaxhighlight lang="python" line='line'>
#!/usr/bin/env python
#!/usr/bin/env python
# -'''- coding: utf-8 -'''-
# -'''- coding: utf-8 -'''-


# DBUS Client using PySide integration
# DBUS Client using PySide2 integration


import sys
import sys
Line 26: Line 24:
import dbus.mainloop.glib
import dbus.mainloop.glib
# import QtCore
# import QtCore
from PySide.QtCore import'''
from PySide2.QtCore import *


# signal handler
# signal handler
def button_clicked():
def button_clicked():
print "button clicked"
    print("button clicked")


# main function
# main function
if ''name'' == '''main''':
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_()


# Enable glib main loop support
</syntaxhighlight>
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_()
</code>


=== D-Bus Server ===
=== D-Bus Server ===
<code>
<syntaxhighlight lang="python" line='line'>
#!/usr/bin/env python
#!/usr/bin/env python
#-'''- coding: utf-8 -'''-
#-'''- coding: utf-8 -'''-


# DBUS Server Example of use PySide with PyDBus library
# DBUS Server Example of use PySide2 with dbus-python library


import dbus
import dbus
Line 76: Line 74:
import random
import random


from PySide.QtCore import'''
from PySide2.QtCore import *
from PySide.QtGui import QPushButton, QApplication
from PySide2.QtWidgets import QPushButton, QApplication


# The adaptor, MUST inherit dbus.service.Object
# The adaptor, MUST inherit dbus.service.Object
class DBusWidget(dbus.service.Object):
class DBusWidget(dbus.service.Object):
def ''init''(self, name, session):
    def __init__(self, name, session):
# export this object to dbus
        # export this object to dbus
dbus.service.Object.''init''(self, name, session)
        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
        # create a simple widget
@dbus.service.signal("com.example.SampleWidget", signature='')
        self.widget = QPushButton()
def lastWindowClosed(self):
        self.widget.resize(200, 50)
pass


if ''name'' == '''main''':
        # To export a Qt signal as a DBus-signal, you need to connect it to
app = QApplication([])
        # a method in this class.
# Use qt/glib mainloop integration to get dbus mainloop working
        # The method MUST have the signal annotation, so dbus-python will
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
        # export it as a dbus-signal
        QObject.connect(self.widget, SIGNAL ("clicked()"), self.clicked)
        QObject.connect(QApplication.instance(), SIGNAL ("lastWindowClosed()"),
            self.lastWindowClosed)


session_bus = dbus.SessionBus()
    # You can export methods to dbus like you do in python-dbus.
# Export the service
    @dbus.service.method("com.example.SampleWidget", in_signature='', out_signature='')
name = dbus.service.BusName("com.example.SampleService", session_bus)
    def show(self):
# Export the object
        self.widget.show()
widget = DBusWidget(session_bus, '/DBusWidget')
   
    # 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


print "Running example service."
if __name__ == "__main__":
app.exec_()
    print("holi")
</code>
    app = QApplication([])
    # Use qt/glib mainloop integration to get dbus mainloop working
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
   
    session_bus = dbus.SessionBus()
    print(session_bus)
    # Export the service
    name = dbus.service.BusName("com.example.SampleService", session_bus)
    # Export the object
    widget = DBusWidget(session_bus, '/DBusWidget')
   
    print("Running
</syntaxhighlight>


=== Running the examples ===
=== Running the examples ===

Latest revision as of 15:11, 27 May 2018


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

#!/usr/bin/env python
# -'''- coding: utf-8 -'''-

# DBUS Client using PySide2 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 PySide2.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_()

D-Bus Server

#!/usr/bin/env python
#-'''- coding: utf-8 -'''-

# DBUS Server Example of use PySide2 with dbus-python library

import dbus
import dbus.service
import dbus.mainloop.glib
import random

from PySide2.QtCore import *
from PySide2.QtWidgets 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 dbus-python 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__":
    print("holi")
    app = QApplication([])
    # Use qt/glib mainloop integration to get dbus mainloop working
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
    
    session_bus = dbus.SessionBus()
    print(session_bus)
    # Export the service
    name = dbus.service.BusName("com.example.SampleService", session_bus)
    # Export the object
    widget = DBusWidget(session_bus, '/DBusWidget')
    
    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.