Change text with PySide and qml, not working for me...



  • Hi,
    I am trying to change the text from "Hello world" to just "Hi". I want to do it by pressing the button "Press me!" that rotates the words "Hello world". I have changed the original example to also change the words not just rotate the text. But the word changing part does not work... What am I doing wrong?

    Error message: "Error: Insufficient arguments"
    It is in the qml part at this line:
    helloText.text = changeText.doChange()

    main.py
    @
    #!/usr/bin/python2.7

    Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).

    All rights reserved.

    Contact: PySide Team (pyside at openbossa.org)

    This file is part of the examples of PySide: Python for Qt.

    ...

    $QT_END_LICENSE$

    from PySide import QtCore, QtGui, QtDeclarative

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

    QtCore.Slot(result=int)
    def val(self):
    

    self.r = self.r + 90
    return self.r

    class ChangeText(QtCore.QObject):
    def init(self):
    super(ChangeText,self).init()
    self.s = "Hi"

    # if a slot returns a value the return value type must be explicitly
    # defined in the decorator
    QtCore.Slot("QString")
    def doChange(self):
        return self.s
    

    if name == 'main':
    import sys

    app = QtGui.QApplication(sys.argv)
    
    view = QtDeclarative.QDeclarativeView()
    
    rotatevalue = RotateValue()
    changeText = ChangeText()
    timer = QtCore.QTimer()
    timer.start(2000)
    
    conrotate = view.rootContext()
    conrotate.setContextProperty("rotatevalue", rotatevalue)
    
    context = view.rootContext()
    context.setContextProperty("changeText", changeText)
    view.setSource(QtCore.QUrl('view.qml'))
    view.show()
    
    sys.exit(app.exec_())
    

    @

    view.qml

    @/*
    ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
    ** All rights reserved.
    ** Contact: PySide Team (pyside at openbossa.org)
    **
    ** This file is part of the examples of PySide: Python for Qt.
    ...
    $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.horizontalCenter: page.horizontalCenter
        y: 120
        MouseArea {
            id: buttonMouseArea
            objectName: "buttonMouseArea"
            anchors.fill: parent
            onClicked: {
                helloText.rotation = rotatevalue.val()
    

    // next line generates the error
    helloText.text = changeText.doChange()
    }
    }
    Text {
    id: buttonText
    text: "Press me!"
    anchors.horizontalCenter: button.horizontalCenter
    anchors.verticalCenter: button.verticalCenter
    font.pointSize: 16;
    }
    }
    }@



  • Hi,

    From looking at http://www.pyside.org/docs/pyside/PySide/QtCore/Slot.html, it seems like QtCore.Slot("QString") will create a slot wanting a QString parameter (rather than a QString return value). Does changing it to something like QtCore.Slot(result='QString') work for you?

    Regards,
    Michael



  • lol thanx man!

    I know I tried that earlier but then I must have had some other error in the code that messed it up :)
    ( they do it in the original example but with a int =) )

    It would have taken me a long time figuring that out :D



  • I absolutely don't understand what you're trying to do here. Do you want to send data from QML to Python or from Python to QML? It looks to me that you totally mixed it.

    I have quite a lot of experiences with both, so tell me what you're trying to do and I'm certain I will be able to help you.



  • Jech: Michael found my problem.

    What I am doing... good question =) I am just playing around and trying to get a basic understanding of the communication.

    I have now changed the doChange function and added a integer attribute to the class. It uses the Class attribute to count how many times I have pressed the button. Not much of use but I have learned how to use a python object in qml.:
    @ QtCore.Slot(str,result="QString")
    def doChange(self,prevMsg):
    if self.i == 0:
    self.originalText= prevMsg
    self.i +=1
    return self.originalText + str(self.i)@
    The next step I will take is to find a way to change any qml element from python. Is there any good tutorials for that?



  • I learned it from the examples provided with PySide. There are basically 2 solutions for this:

    1. Create a QObject with a property and in QML use this property. Whenever you change the value, you emit "changed" signal and this will instruct the QML UI to react upon it. This is the preferred way of doing it.

    2. You can call a JavaScript function in your QML from Python. In the function you can do whatever you want. This is needed in some cases, where you explicitly need to call a function not just change a property.

    If you need more help, let me know.



  • Thank you, I have got it to work now :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.