Connecting QML signals in PySide

This page describes a few alternative approaches for connecting signals between QML and PySide. Simple illustrative examples about the signal connectivity are also provided in the pyside-examples repository [qt.gitorious.org]. Browse the git tree directly or download a tarball of all examples [qt.gitorious.org] and refer to the examples/declarative/signals directory.

Connecting signals from QML to Python

Connecting a signal from QML to Python is the most common use case. This allows for example connecting button clicks and other user interface events in QML to the backend logic written in Python.

There are multiple alternative methods for connecting QML signals to Python. The methods are not mutually exclusive; any of them can be used in a single program (and even for a single signal, if need be).

Explicitly calling a Python slot from QML

If the Python object is exposed to QML using setContextProperty, you can call any slot of the object explicitly from QML, as shown in qmltopy1 [qt.gitorious.org]. First, you define a class in Python, inheriting from QObject:

  1. class Console(QtCore.QObject):
  2.     @QtCore.Slot(str)
  3.     def outputStr(self, s):
  4.         print s

Then, the Python object is instantiated and connected to the QML context:

  1. con = Console()
  2. view = QtDeclarative.QDeclarativeView()
  3. context = view.rootContext()
  4. context.setContextProperty("con", con)

After this, the object is accessible in QML and any slot can be called just like a function:

  1. MouseArea {
  2.     onClicked: {
  3.         con.outputStr("Hello, world!")
  4.     }
  5. }

Returning a value from a slot

It is possible for slots to return values to the QML caller (see example qmltopy2 [qt.gitorious.org]). In this case, the Python slot needs to define an explicit return type:

  1.     @QtCore.Slot(result=int)
  2.     def val(self):
  3.         self.r = self.r + 10
  4.         return self.r

Then the Python object is exposed to QML, after which it can be called just like a function:

  1.             onClicked: {
  2.                 helloText.rotation = rotatevalue.val()
  3.             }

Connecting signals from QML to Python using a top-level QML signal

If you prefer to handle the signal connection in Python, the simplest way to do it is to declare a top-level signal in QML and connect that to a Python slot as shown in example qmltopy3 [qt.gitorious.org].

First, in the top-level QML item, declare the signal:

  1. Rectangle {
  2.     id: page
  3.  
  4.     signal textRotationChanged(double rot)
  5.  
  6.     [...]

Then, in some other QML item, make some other signal handler emit this signal:

  1.         onRotationChanged: textRotationChanged(rotation)

Finally, in Python, acquire the QML root object and connect the signal:

  1.     root = view.rootObject()
  2.     root.textRotationChanged.connect(sayThis)

This approach has the benefit of hiding the Python class behaviour from QML. On the other hand, this approach requires the Python code to have some knowledge of the QML item contents.

Connecting a signal from a specific QML item to Python.

It is also possible to acquire a specific QML item and connect a signal directly to it as illustrated in example qmltopy4 [qt.gitorious.org].

In principle, this approach doesn’t require any extra consideration in QML. Unfortunately, in practice it may be difficult to find the proper QML items without assigning them object names:

  1.         MouseArea {
  2.             id: buttonMouseArea
  3.             objectName: "buttonMouseArea"
  4.             anchors.fill: parent
  5.         }

Then, in Python, it is simple to find the desired item using findChild and connect a signal to it:

  1.     button = root.findChild(QtCore.QObject,"buttonMouseArea")
  2.     button.clicked.connect(lambda: sayThis("clicked button (signal directly connected)"))

Although initially tempting, this approach ties the QML and Python codebases pretty tightly to each other. Also, having to define the object names in QML makes the approach less than optimal.

Connecting signals from Python to QML

It is also possible to connect signals from Python to QML when e.g. changes in the model data need to be reflected in the UI. Example pytoqml1 [qt.gitorious.org] illustrates how to do that.

First, a function is defined in QML:

  1.     function updateRotater() {
  2.         rotater.angle = rotater.angle + 45
  3.     }

QML functions appear as slots in Python, so it’s a simple matter to connect a signal to them:

  1.     timer.timeout.connect(root.updateRotater)

This allows for fairly clean and straightforward delivery of signals from Python to QML.

Categories: