May 31, 2012

aekam aekam
Ant Farmer
87 posts

switch case problem

 

Hello all,

in QML is it necessary to wrap body of case in curly brackets.??
let’s say,

  1. switch (arg) {
  2. case match1: {
  3. step1
  4. step2
  5. step3
  6. }
  7. break
  8.  
  9. case match2: {
  10. step1
  11. step2
  12. step3
  13. }
  14. break
  15.  
  16. default: {
  17. step1
  18. step2
  19. step3
  20. }
  21. break
  22. }

because if i don’t use brackets, execution of case is carried out only for first step and remaining steps are skipped…

 Signature 

If you take care of inches, you won’t have to worry about miles… :)

5 replies

June 1, 2012

chriadam chriadam
Ant Farmer
181 posts

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.

June 1, 2012

aekam aekam
Ant Farmer
87 posts

if i write,

  1. Item {
  2.      width: 100
  3.      height: 100
  4.  
  5.      property int count: 0
  6.  
  7.      Component.onCompleted: {
  8.  
  9.           switch (count) {
  10.           case 0:
  11.                console.log ("1: count is zero")
  12.                console.log ("2: count is zero")
  13.                console.log ("3: count is zero")
  14.                break
  15.           default:
  16.                console.log ("1: count is non zero")
  17.                console.log ("2: count is non zero")
  18.                console.log ("3: count is non zero")
  19.                break
  20.           }
  21.      }
  22. }

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.

 Signature 

If you take care of inches, you won’t have to worry about miles… :)

June 1, 2012

Dickson Dickson
Lab Rat
22 posts

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

June 7, 2012

chriadam chriadam
Ant Farmer
181 posts

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.

June 18, 2012

michal.k michal.k
Lab Rat
70 posts

Hi,

This problem was mentioned in this [qt-project.org] post

 
  ‹‹ [solved] QDeclarativeItem geometry      [solved]QML component: Cannot assign multiple values to a singular property ››

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