[PySide] QML calling variable argument function

259 views
Skip to first unread message

daniel....@nokia.com

unread,
Aug 18, 2011, 12:54:59 PM8/18/11
to pys...@lists.pyside.org
I'd like to make a slot in my pyside code that accepts any arguments so I can call it in a variety of situations from QML. I was hoping that something like this would work:

Python:

view.rootContext().setContextProperty('test', view)

...

@Slot(object)
def stuff(self, o):
print o

QML:

...
MouseArea
{
onClick:
{
test.stuff('hello')
test.stuff([1,2,3])
}
}

Ideally, there would some sort of variable arguments I could use so that I could have 'def stuff(self, *args)' and call in QML 'test.stuff(1,2,3)'.

Any advice?


dan

----
Daniel Ashbrook, PhD
Senior Researcher, New Mobile Forms and Experiences
Nokia Research Center
Media Technologies Lab, Santa Monica
daniel....@nokia.com

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

Thomas Perl

unread,
Aug 18, 2011, 3:19:35 PM8/18/11
to daniel....@nokia.com, pys...@lists.pyside.org
Hi Daniel,

2011/8/18 <daniel....@nokia.com>:


> I'd like to make a slot in my pyside code that accepts any arguments so I can call it in a variety of situations from QML. I was hoping that something like this would work:

> [...]


>  view.rootContext().setContextProperty('test', view)
> [...]
>  @Slot(object)
>  def stuff(self, o):
>    print o

> [...]


>      test.stuff('hello')
>      test.stuff([1,2,3])

> [...]


> Ideally, there would some sort of variable arguments I could use so that I could have 'def stuff(self, *args)' and call in QML 'test.stuff(1,2,3)'.
>
> Any advice?

Does Qt have variable-argument slots? I ask because I'm not sure if
it's technically feasible. I only found a thread from 2009 that says
it is not possible:

http://www.qtcentre.org/threads/26014-slot-with-variable-arguments

At least using the [1,2,3] as QScriptValue and then doing some
"unpacking" of this on the Python side should be possible.

Another possibility I see is to subclass QScriptValue and overwrite
the call() method on it:

http://doc.qt.nokia.com/latest/qscriptvalue.html#call

I have not tested this myself, and I wouldn't be surprised if it
needed some fixes in the PySide engine - just listing some
possibilities :)

HTH.
Thomas

daniel....@nokia.com

unread,
Aug 18, 2011, 4:25:40 PM8/18/11
to th....@gmail.com, pys...@lists.pyside.org
Well, what I ended up doing is this:

In QML:

Rectangle
{
signal my_func(variant args)
[...]
MouseArea
{
[...]
onClicked: my_func([1,2,3])
}
}

and in Python:

def stuff(a):
print a

[...]

view.rootObject().my_func.connect(stuff)

which works pretty well for my purposes. It'd be more convenient to have a variable list of args, but I'm happy enough. =)

Thanks for the ideas!


dan

----
Daniel Ashbrook, PhD
Senior Researcher, New Mobile Forms and Experiences
Nokia Research Center
Media Technologies Lab, Santa Monica
daniel....@nokia.com

Reply all
Reply to author
Forward
0 new messages