Comma Expressions Good or Bad?
Unsolved
QML and Qt Quick
-
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 } }
-
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 } }