Results 1 to 4 of 4

Thread: In which case should the `if` condition be used and in which case be `when` condition

  1. #1
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default In which case should the `if` condition be used and in which case be `when` condition

    http://qt-project.org/doc/qt-5.0/qtq...-when-property

    Below is a ***when*** statement usage:
    For convenience, the State element has a when property that can bind to expressions to change the state whenever the bound expression evaluates to true. The when property will revert the state back to the default state when the expression evaluates to false.

    Rectangle {
    id: bell
    width: 75; height: 75
    color: "yellow"

    states: State {
    name: "RINGING"
    when: (signal.state == "CRITICAL")
    PropertyChanges {target: speaker; play: "RING!"}
    }
    }

    The bell component will change to the RINGING state whenever the signal.state is CRITICAL.
    ----------


    Below is a ***if*** statement usage:

    Qt Code:
    1. onClicked: rectangleA.state == "one" ?
    2. rectangleA.state = "two" : rectangleA.state = "one"
    To copy to clipboard, switch view to plain text mode 

    Question: In which case should the if condition be used and in which case be when condition be used?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: In which case should the `if` condition be used and in which case be `when` condi

    There is no rule. Use whatever seems clearer for you.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: In which case should the `if` condition be used and in which case be `when` condi

    Is there "any" advantage to "when" as compared to "if"?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: In which case should the `if` condition be used and in which case be `when` condi

    There is one that transitions are defined in a single place (inside state definitions) instead of being scattered all over code.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. convert upper-case to lower-case
    By RENOLD in forum Qt Programming
    Replies: 2
    Last Post: 23rd February 2012, 08:30
  2. QKeyEvent - lower case vs upper case keys?
    By ArkKup in forum Qt Programming
    Replies: 3
    Last Post: 9th January 2012, 16:58
  3. Using an If condition in a QML delegate?
    By technoViking in forum Qt Quick
    Replies: 8
    Last Post: 25th August 2011, 06:26
  4. how to set wait condition
    By hema in forum Newbie
    Replies: 5
    Last Post: 25th July 2011, 09:08
  5. don`t work Condition...
    By Resager in forum Newbie
    Replies: 1
    Last Post: 6th May 2011, 19:18

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.