[Qt-qml] Javascript behaviour

58 views
Skip to first unread message

Harri Pasanen

unread,
Jan 26, 2012, 4:02:36 PM1/26/12
to qt-...@qt.nokia.com
Hi,

Perhaps like most javascript coders, I'm mainly educated
by experimenting with it.

I have a pretty strong Python background though, and
most of the time I can carry over some usage patterns
just guessing how it probably works in js/qml side.

Now I was surprised though, and I wonder if the following behaviour
is a bug, or what is going on.

I have some code, roughly like

A.qml:
Item {
property variant cb // callback

signal close()

onClosed: cb()

function popup(callback) {
cb = callback // callback() is called in this assignment!?
}

...
}


then some
B.qml:

Item {
A { id: hello }

function continueHere() {
print "should continue here"
}

function setup() {
hello.popup(continueHere)
}
}


My problem is that callback is called at assignment time (see comment in
code).
Is it an artifact of cb being a variant property and I should not be
doing this?


Confused, any ideas?

Thanks,

Harri

_______________________________________________
Qt-qml mailing list
Qt-...@qt.nokia.com
http://lists.qt.nokia.com/mailman/listinfo/qt-qml

christop...@nokia.com

unread,
Jan 26, 2012, 6:45:21 PM1/26/12
to ha...@mpaja.com, qt-...@qt.nokia.com
Hi Harri,

There are a couple of reasons why this doesn't work for your code:
Firstly, the property is a variant property (which internally is a QVariant and so cannot store a JavaScript function value).
Secondly, (and the real reason), we've currently defined the semantic that assigning a function to a property causes a binding assignment. A binding assignment causes the bound expression to be evaluated, and all of the properties involved in the expression to be "captured" (which means we automatically re-evaluated the expression if any of those properties change).

In short, the behavior you are seeing is expected.

However, for QtQuick2.0, we are looking at ways to allow a function value to be saved to a "var" property (or perhaps to a new property type). We are currently considering several options:

1) adding a new property type: "property function someProp" to which js function values can be assigned
2) adding a new function to the Qt global object: Qt.assignableFunction("expr") -- if the result is assigned to a var property, it is NOT considered a binding assignment
3) changing the semantic so that a function assignment is NOT considered to be a binding assignment, and instead adding a new function to the Qt global object: Qt.binding("expr") -- if the result is assigned to any property, it is evaluated as a binding assignment.

Note that we do need some way to define binding assignments (eg for initial property values in dynamic object instantiation (loader, component), and for generating bindings in imperative js code), but as you point out, there are some good use-cases for being able to assign a js function value to a var property.

The current workaround is to store a js array with a single element (the function value) to a var property. But obviously that's not a desirable situation.

Cheers,
Chris.

Bo Thorsen

unread,
Jan 27, 2012, 1:26:11 AM1/27/12
to qt-...@qt.nokia.com
Another way to implement this is to do it in a separate javascript file.
There you can use all the javascript you need, and not the "almost
javascript" of QML.

I find myself using those whenever I do real programming in javascript.
The inline JS in QML is fine for short things, but for real work, I use
those.

And remember to use ".pragma library" so it's the same scope for all QML
files.

Bo.


Bo Thorsen,
Fionia Software.

--

Expert Qt and C++ developer for hire
Contact me if you need expert Qt help
http://www.fioniasoftware.dk

Harri Pasanen

unread,
Jan 27, 2012, 9:41:28 AM1/27/12
to christop...@nokia.com, qt-...@qt.nokia.com

On 01/27/2012 12:45 AM, christop...@nokia.com wrote:
> Hi Harri,


>
>
> In short, the behavior you are seeing is expected.
>
> However, for QtQuick2.0, we are looking at ways to allow a function value to be saved to a "var" property (or perhaps to a new property type). We are currently considering several options:
>
> 1) adding a new property type: "property function someProp" to which js function values can be assigned
> 2) adding a new function to the Qt global object: Qt.assignableFunction("expr") -- if the result is assigned to a var property, it is NOT considered a binding assignment
> 3) changing the semantic so that a function assignment is NOT considered to be a binding assignment, and instead adding a new function to the Qt global object: Qt.binding("expr") -- if the result is assigned to any property, it is evaluated as a binding assignment.
>
> Note that we do need some way to define binding assignments (eg for initial property values in dynamic object instantiation (loader, component), and for generating bindings in imperative js code), but as you point out, there are some good use-cases for being able to assign a js function value to a var property.

Thanks for the explanation, makes sense.

Of the 3 alternatives I prefer the first one on style grounds.
2 & 3 sound "hacky", if the Qt.xxx is elsewhere in
code it is not obvious for someone reading the code that it
even exists.

Changing the semantics of assignment elsewhere
in code sounds odd. Probably less odd for C++ users
for whom it is more common, but the target developer
is probably not him.

Reply all
Reply to author
Forward
0 new messages