Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Semicolons in QML



  • Hey guys, just a general question here about the use of semicolons. Now, from what I understand in JS the semicolon is optional save for a few special cases. Is the same true for QML? For instance, take the following code snip:

      Component.onCompleted:
      {
        state = "selection";
      }
    

    this seems to work exactly the same as:

      Component.onCompleted: state = "selection"
    

    is there a valid argument for the first case over the second in QML?



  • @Circuits said in Semicolons in QML:

    Hey guys, just a general question here about the use of semicolons. Now, from what I understand in JS the semicolon is optional save for a few special cases. Is the same true for QML?

    Yes

    For instance, take the following code snip:
    Component.onCompleted:
    {
    state = "selection";
    }

    this seems to work exactly the same as:
    Component.onCompleted: state = "selection"

    It is exactly the same.

    is there a valid argument for the first case over the second in QML?

    No, only developer preferences.

    With semi-colons you can add more commands on one line.
    Brackets are required when using multiple commands for a connection on a signal like Component.onCompleted. For single commands they are optional.


  • Moderators

    @Circuits said in Semicolons in QML:

    is there a valid argument for the first case over the second in QML?

    Note: The code to the right-hand-side of : is a JavaScript statement. So, you follow the JavaScript rules of braces and semicolons.

    The following snippets are all exactly the same. Like @KroMignon said, just choose the one you prefer.

    // Style 1
    Component.onCompleted:
    {
        state = "selection";
    }
    
    // Style 2
    Component.onCompleted:
    {
        state = "selection"
    }
    
    // Style 3
    Component.onCompleted: { state = "selection"; }
    
    // Style 4
    Component.onCompleted: { state = "selection" }
    
    // Style 5
    Component.onCompleted: state = "selection";
    
    // Style 6
    Component.onCompleted: state = "selection"
    

Log in to reply