Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Switch case problem
Qt 6.11 is out! See what's new in the release blog

Switch case problem

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 4 Posters 14.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aekam
    wrote on last edited by
    #1

    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
    }
    break

    case match2: {
    step1
    step2
    step3
    }
    break

    default: {
    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...

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

    1 Reply Last reply
    0
    • C Offline
      C Offline
      chriadam
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        aekam
        wrote on last edited by
        #3

        if i write,

        @
        Item {
        width: 100
        height: 100

         property 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.

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

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dicksonleong
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • C Offline
            C Offline
            chriadam
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MichK
              wrote on last edited by
              #6

              Hi,

              This problem was mentioned in "this":http://qt-project.org/forums/viewthread/9897/ post

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved