December 20, 2010

jech jech
Lab Rat
73 posts

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 [qt.gitorious.org]. But it only shows how to call a function of the root object, but how do I call function(s) of other QML objects?

6 replies

December 23, 2010

blam blam
Lab Rat
57 posts

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

December 23, 2010

QtK QtK
Lab Rat
1140 posts

blam wrote:
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().

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

December 23, 2010

jech jech
Lab Rat
73 posts

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

December 23, 2010

QtK QtK
Lab Rat
1140 posts
jech wrote:
QtK: So what would be the recommended way of calling a function of a certain QML object?

This [mail-archive.com] thread should help you.

December 23, 2010

jech jech
Lab Rat
73 posts

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?

May 20, 2011

FxIII FxIII
Lab Rat
1 posts

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.

 
  ‹‹ BorderImages and transparency in images      .bin file from qml ››

You must log in to post a reply. Not a member yet? Register here!