1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
#
4
# Study of signaling from QML to Python and back to QML.
5
# The third signal "sc" to slot "s3" emits a signal with an url
6
# that the webkit view opens. 
7
import sys, signal
8
9
from PySide import QtGui, QtCore
10
from PySide.QtDeclarative import QDeclarativeView
11
12
13
class Po(QtCore.QObject):
14
    def __init__(self):
15
        QtCore.QObject.__init__(self)
16
17
    @QtCore.Slot()
18
    def s1(self):
19
        self._echo('S1: This is called without any argument')
20
21
    @QtCore.Slot(int)
22
    def s2(self,i):
23
        self._echo('S2: This is called with the number "%d"'%i)
24
25
    @QtCore.Slot('QString')
26
    def s3(self,s):
27
        self._echo('S3: This is called with a string: %s'%s)
28
        url = 'http://pyside.org'
29
        self.url_to_qml.emit(url)
30
31
    def _echo(self,s):
32
        QtGui.QMessageBox.information(None, "Info", s)
33
        print s
34
35
    url_to_qml = QtCore.Signal('QVariant')
36
    
37
38
39
if __name__ == "__main__":
40
    # initiate the main QML application
41
    app = QtGui.QApplication(sys.argv)
42
    view = QDeclarativeView()
43
    view.setResizeMode(QDeclarativeView.SizeRootObjectToView)
44
45
    ctx = view.rootContext()
46
    # set html content
47
    ctx.setContextProperty("htmlContent", "press the button to trigger signals from QML")
48
49
    url = QtCore.QUrl('webview.qml')
50
    view.setSource(url)
51
52
    po=Po()
53
    root = view.rootObject()
54
55
    QtCore.QObject.connect(
56
        root, QtCore.SIGNAL('sa()'),
57
        po, QtCore.SLOT('s1()'))
58
    QtCore.QObject.connect(
59
        root, QtCore.SIGNAL('sb(int)'),
60
        po, QtCore.SLOT('s2(int)'))
61
    QtCore.QObject.connect(
62
        root, QtCore.SIGNAL('sc(QString)'),
63
        po, QtCore.SLOT('s3(QString)'))
64
    QtCore.QObject.connect(
65
        po, QtCore.SIGNAL('url_to_qml(QVariant)'),
66
        root, QtCore.SLOT('dataReceived(QVariant)'))
67
68
    view.show()
69
    
70
    # ensure that the application quits using CTRL-C
71
    signal.signal(signal.SIGINT, signal.SIG_DFL)
72
73
    # bring the app up front on OS X
74
    view.raise_()
75
76
    # execute
77
    sys.exit(app.exec_())