Switch case problem
-
wrote on 31 May 2012, 13:22 last edited by
Hello all,
in QML is it necessary to wrap body of case in curly brackets.??
let's say,
@
switch (arg) {
case match1: {
step1
step2
step3
}
breakcase match2: {
step1
step2
step3
}
breakdefault: {
step1
step2
step3
}
break
}
@because if i don't use brackets, execution of case is carried out only for first step and remaining steps are skipped...
-
wrote on 1 Jun 2012, 00:36 last edited by
This is probably a bug in the expression rewriter. Basically, in QML, most JavaScript expressions pass through an expression rewriter (depending on the situation; eg, a binding like: property int a: {some expression} gets rewritten as property int a: (function(){return {some expression} }).
There are a few things in JavaScript which are tricky - switch statements and the "with" statement spring to mind immediately.
If you have a simple reproduction for this issue, please create a bug report and assign it to me, I'll take a look at it.
Cheers,
Chris. -
wrote on 1 Jun 2012, 04:24 last edited by
if i write,
@
Item {
width: 100
height: 100property int count: 0 Component.onCompleted: { switch (count) { case 0: console.log ("1: count is zero") console.log ("2: count is zero") console.log ("3: count is zero") break default: console.log ("1: count is non zero") console.log ("2: count is non zero") console.log ("3: count is non zero") break } }
}
@here i haven't used any brackets and hence i am not getting 2nd and 3rd console log prints, if i use brackets it prints.
-
wrote on 1 Jun 2012, 16:18 last edited by
same here, in a .qml file, only the first line in a case block will be execute. However, in a .js file there is no such problem
-
wrote on 6 Jun 2012, 23:30 last edited by
Yeah, the contents of .js file imports are (after .pragmas and .imports are stripped out) passed directly to a JavaScript engine, and thus don't suffer the same "rewriting" fate as expressions in .qml files.
Thanks for the simple repro, I'll look into this.
-
wrote on 18 Jun 2012, 08:15 last edited by
Hi,
This problem was mentioned in "this":http://qt-project.org/forums/viewthread/9897/ post