[PySide] Calling a Python function from QML and vice versa

887 views
Skip to first unread message

Vladimír Jícha

unread,
Dec 20, 2010, 10:32:41 AM12/20/10
to pys...@lists.openbossa.org
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

main.py
view.qml

Hugo Parente Lima

unread,
Dec 20, 2010, 10:25:41 AM12/20/10
to pys...@lists.openbossa.org
On Monday 20 December 2010 13:32:41 Vladimír Jícha wrote:
> 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?

val2() *must* be declared as a slot to be visible on QML.



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

You do it the same way you do to call javascript functions from C++.

> Thank you,
> Vladimir

--
Hugo Parente Lima
INdT - Instituto Nokia de Tecnologia

signature.asc

Vladimír Jícha

unread,
Dec 20, 2010, 4:21:04 PM12/20/10
to Hugo Parente Lima, pys...@lists.openbossa.org
Thanks a lot for your quick answer, Hugo.

Dne 20.12.2010 16:25, Hugo Parente Lima napsal(a):


> On Monday 20 December 2010 13:32:41 Vladimír Jícha wrote:
>
>> 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?
>>
> val2() *must* be declared as a slot to be visible on QML.
>

Thank you, now it works fine.

>
>
>> 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?
>>
> You do it the same way you do to call javascript functions from C++.
>

I'm sorry for my ignorance, but unfortunately I have no knowledge of
C++. Could you give me a short code sample, please? Thank you.

Vladimir
_______________________________________________
PySide mailing list
PyS...@lists.openbossa.org
http://lists.openbossa.org/listinfo/pyside

Hugo Parente Lima

unread,
Dec 20, 2010, 3:34:31 PM12/20/10
to Vladimír Jícha, pys...@lists.openbossa.org
On Monday 20 December 2010 19:21:04 Vladimír Jícha wrote:
> Thanks a lot for your quick answer, Hugo.
>
> Dne 20.12.2010 16:25, Hugo Parente Lima napsal(a):
> > On Monday 20 December 2010 13:32:41 Vladimír Jícha wrote:
> >> 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?
> >
> > val2() *must* be declared as a slot to be visible on QML.
>
> Thank you, now it works fine.
>
> >> 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?
> >
> > You do it the same way you do to call javascript functions from C++.
>
> I'm sorry for my ignorance, but unfortunately I have no knowledge of
> C++. Could you give me a short code sample, please? Thank you.

The example (a unit test from pyside) is attached, just replace the
adjust_filename function by nothing, i.e. just the QML file name.

bug_451.py
bug_451.qml
signature.asc

Vladimír Jícha

unread,
Dec 21, 2010, 8:10:24 AM12/21/10
to Hugo Parente Lima, pys...@lists.openbossa.org

Dne 20.12.2010 21:34, Hugo Parente Lima napsal(a):


> On Monday 20 December 2010 19:21:04 Vladim�r J�cha wrote:
>
>> Thanks a lot for your quick answer, Hugo.
>>
>> Dne 20.12.2010 16:25, Hugo Parente Lima napsal(a):
>>

>>> On Monday 20 December 2010 13:32:41 Vladim�r J�cha wrote:
>>>
>>>> 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?
>>>>
>>> val2() *must* be declared as a slot to be visible on QML.
>>>
>> Thank you, now it works fine.
>>
>>
>>>> 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?
>>>>
>>> You do it the same way you do to call javascript functions from C++.
>>>
>> I'm sorry for my ignorance, but unfortunately I have no knowledge of
>> C++. Could you give me a short code sample, please? Thank you.
>>
> The example (a unit test from pyside) is attached, just replace the
> adjust_filename function by nothing, i.e. just the QML file name.
>
>

Thank you, I understand it now better. But still I don't know how to
call a function of other then root QML object. For example if I modified
the QML (please check the attachment), how would I call the
"returnFunction()" now? Thank you.

Vladimir

bug_451.qml

Hugo Parente Lima

unread,
Dec 21, 2010, 9:24:55 AM12/21/10
to Vladimír Jícha, pys...@lists.openbossa.org
On Tuesday 21 December 2010 11:10:24 Vladimír Jícha wrote:
> Dne 20.12.2010 21:34, Hugo Parente Lima napsal(a):
> > On Monday 20 December 2010 19:21:04 Vladimír Jícha wrote:
> >
> > The example (a unit test from pyside) is attached, just replace the
> > adjust_filename function by nothing, i.e. just the QML file name.
>
> Thank you, I understand it now better. But still I don't know how to
> call a function of other then root QML object. For example if I modified
> the QML (please check the attachment), how would I call the
> "returnFunction()" now? Thank you.

This is more a QML doubt rather than a PySide one, I'm not a QML expert so I
would try to set a property alias to the Text object because I don't have any
clue about how to get a QDeclarativeContext different from the root context
using the QML API.

signature.asc
Reply all
Reply to author
Forward
0 new messages