Important: Please read the Qt Code of Conduct -

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()

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

    All rights reserved.

    Contact: PySide Team (pyside at

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



    from PySide import QtCore, QtGui, QtDeclarative

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

    def val(self):

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

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

    # if a slot returns a value the return value type must be explicitly
    # defined in the decorator
    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()
    conrotate = view.rootContext()
    conrotate.setContextProperty("rotatevalue", rotatevalue)
    context = view.rootContext()
    context.setContextProperty("changeText", changeText)



    ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
    ** All rights reserved.
    ** Contact: PySide Team (pyside at
    ** This file is part of the examples of PySide: Python for Qt.

    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, 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?


  • 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