Calling QML function from PySide



  • Hello,

    I'd like to get help on calling a function of a QML object from PySide. I know the example "here":http://qt.gitorious.org/pyside/pyside-examples/blobs/master/examples/declarative/signals/pytoqml1/main.py. But it only shows how to call a function of the root object, but how do I call function(s) of other QML objects?



  • If you need to locate a child object, you should be able to use QObject::findChild(). The C++ example is here: http://doc.qt.nokia.com/4.7-snapshot/qtbinding.html#locating-child-objects

    Basically you'll need to set the 'objectName' for the child in order to locate it using QObject::findChild().



  • [quote author="blam" date="1293063077"]If you need to locate a child object, you should be able to use QObject::findChild(). The C++ example is here: http://doc.qt.nokia.com/4.7-snapshot/qtbinding.html#locating-child-objects

    Basically you'll need to set the 'objectName' for the child in order to locate it using QObject::findChild().[/quote]

    But this is usually not recommended right. Setting the objectName.



  • QtK: So what would be the recommended way of calling a function of a certain QML object?



  • [quote author="jech" date="1293089959"]QtK: So what would be the recommended way of calling a function of a certain QML object?[/quote]

    "This":http://www.mail-archive.com/qt-qml@trolltech.com/msg01464.html thread should help you.



  • Thank you, this information helped me a lot. I think I can use a QObject with it's properties in most cases.

    But still I think I need to call a function in some cases. For example I need to set currentIndex of a ListView from Python. How would I do that?



  • This question raise a design issue; while is not a very good idea to pollute your root object with tons of functions, is still a good idea to put to root object all the function you need.
    You should try to think about root object as the interface to your qml gui: the deep you go through your QmlItems the wrost is going to be.
    You should expose a clear interface on your root object so changes behind the scenes can be kept hidden.
    In your case you should write a search function on the root object that parametrically finds what you want; that function should be semantc: say what you want and i will give it to you.
    Once again changing the entire qml and providing the same interface, gives you a new way to present and do stuffs, without change the backend.
    Doing so, you may see you are going to write too much function on your root object. In this case you can consider to write some interface Item on your Qml code directly behind your root object. So you can broke down interfaces by semantically unrelated groups and provide those interfaces as main gateways through your root object.
    I hope this can clear your picture.


Log in to reply
 

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