Hi List,

It's me again with some QML related questions. I'm trying to call a function in my Python code from QML UI. I followed the example called "qmltopy2". It works OK. But I wanted more functions with the same input parameters. I thought I only need to write a new function and then call it from QML. But apparently this doesn't work. Please see my modified version of the qmltopy2 example.

Is this a bug or is the behavior intended? If it's intended, how to do it right?

And talking aout calling Python functions from QML, what about the other way - calling a QML (JavaScript) funcion from Python? I studied the pytoqml1 eample. It shows how to call a function of QML root object. How do I call functions of other objects?

Thank you,
Vladimir
#!/usr/bin/python

# Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
# All rights reserved.
# Contact: PySide Team ([email protected])
#
# This file is part of the examples of PySide: Python for Qt.
#
# $QT_BEGIN_LICENSE:BSD$
# You may use this file under the terms of the BSD license as follows:
#
# "Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#   * Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in
#     the documentation and/or other materials provided with the
#     distribution.
#   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
#     the names of its contributors may be used to endorse or promote
#     products derived from this software without specific prior written
#     permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
# $QT_END_LICENSE$

from PySide import QtCore, QtGui, QtDeclarative

class RotateValue(QtCore.QObject):
    def __init__(self):
        super(RotateValue,self).__init__()
        self.r = 0

    # if a slot returns a value the return value type must be explicitly
    # defined in the decorator
    @QtCore.Slot(result=int)
    def val(self):
        self.r = self.r - 10
        return self.r

    def val2(self):
        self.r = self.r + 10
        return self.r

if __name__ == '__main__':
    import sys

    app = QtGui.QApplication(sys.argv)

    view = QtDeclarative.QDeclarativeView()

    rotatevalue = RotateValue()

    timer = QtCore.QTimer()
    timer.start(2000)

    context = view.rootContext()
    context.setContextProperty("rotatevalue", rotatevalue)

    view.setSource(QtCore.QUrl('view.qml'))
    view.show()

    sys.exit(app.exec_())

/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: PySide Team ([email protected])
**
** This file is part of the examples of PySide: Python for Qt.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
**     the names of its contributors may be used to endorse or promote
**     products derived from this software without specific prior written
**     permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/

import Qt 4.7

Rectangle {
    id: page

    width: 500; height: 200
    color: "lightgray"

    Text {
        id: helloText
        text: "Hello world!"
        anchors.horizontalCenter: page.horizontalCenter
        y: 30
        font.pointSize: 24; font.bold: true
    }


    Rectangle {
        id: button
        width: 150; height: 40
        color: "darkgray"
        anchors.left: page.left
        anchors.leftMargin: 50
        y: 120
        MouseArea {
            id: buttonMouseArea
            objectName: "buttonMouseArea"
            anchors.fill: parent
            onClicked: {
                helloText.rotation = rotatevalue.val()
            }
        }
        Text {
            id: buttonText
            text: "Rotate left"
            anchors.horizontalCenter: button.horizontalCenter
            anchors.verticalCenter: button.verticalCenter
            font.pointSize: 16;
        }
    }

    Rectangle {
        id: button2
        width: 150; height: 40
        color: "darkgray"
        anchors.right: page.right
        anchors.rightMargin: 50
        y: 120
        MouseArea {
            id: buttonMouseArea2
            objectName: "buttonMouseArea2"
            anchors.fill: parent
            onClicked: {
                helloText.rotation = rotatevalue.val2()
            }
        }
        Text {
            id: buttonText2
            text: "Rotate right"
            anchors.horizontalCenter: button2.horizontalCenter
            anchors.verticalCenter: button2.verticalCenter
            font.pointSize: 16;
        }
    }
}
_______________________________________________
PySide mailing list
[email protected]
http://lists.openbossa.org/listinfo/pyside

Reply via email to