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. Comma Expressions Good or Bad?
QtWS25 Last Chance

Comma Expressions Good or Bad?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 1 Posters 445 Views
  • 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.
  • F Offline
    F Offline
    fcarney
    wrote on 8 Aug 2022, 19:40 last edited by
    #1

    If I use a value in an expression with a comma I can force a property to update regardless if the data after the comma changes. This is useful in some cases where you want the event the on<property>Changed to fire. The warning says don't use them. I have found them in use in code that Qt provides (I think in TreeView from Controls 1). Is this really bad practice? Is there another way to force properties to fire the change event?

    import QtQuick 2.15
    import QtQuick.Window 2.15
    import QtQuick.Controls 2.15
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Property update with comma")
    
        // get warning saying "Do not use comma expressions. (M30)"
        readonly property var listStuff: checkbox.checked, [0,1,2,3,4,5]
        onListStuffChanged: {
            console.log("listStuff changed", listStuff)
        }
    
        CheckBox {
            id: checkbox
        }
    }
    

    C++ is a perfectly valid school of magic.

    1 Reply Last reply
    0
    • F Offline
      F Offline
      fcarney
      wrote on 8 Aug 2022, 21:37 last edited by
      #2

      It looks like you can create dependencies in functions with a bit different syntax. It also looks like there is an implied return if the return keyword is not used:

      import QtQuick 2.15
      import QtQuick.Window 2.15
      import QtQuick.Controls 2.15
      
      Window {
          width: 640
          height: 480
          visible: true
          title: qsTr("Property update with comma")
      
          // get warning saying "Do not use comma expressions. (M30)"
          readonly property var listStuff: checkbox.checked, [0,1,2,3,4,5]
          onListStuffChanged: {
              console.log("listStuff changed", listStuff)
          }
      
          readonly property var listStuff2: {
              checkbox.checked
              return [5,4,3,2,1,0]
          }
          onListStuff2Changed: {
              console.log("listStuff2 changed", listStuff2)
          }
      
          readonly property var listStuff3: { checkbox.checked; [0,1,2,3,2,1,0] }
          onListStuff3Changed: {
              console.log("listStuff3 changed", listStuff3)
          }
      
          CheckBox {
              id: checkbox
          }
      }
      

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      0

      2/2

      8 Aug 2022, 21:37

      • Login

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