When to use ':' versus '='?



  • I'm learning QML. I'm playing with an application that uses a ListView, and I'm using a delegate. In all of this QML, properties are set using a ':'.

    I wanted to know how to do something when a delegate was tapped, and I found some code that does it. But in that code is the line

    list_view1.currentIndex = index
    

    My question is why in this case is '=' used instead of ':'? And more generally, when is each appropriate? I can't seem to find this in the documentation.

    Thanks.


  • Lifetime Qt Champion

    Hi,

    Because it's assigning index to currentIndex, it's not setting its property.

    : is used when you set the properties when declaring the object. For the rest of the code, you can't do that.


  • Moderators

    Hi,

    @treaves said:

    My question is why in this case is '=' used instead of ':'?

    Because that is JavaScript code, and JavaScript only lets you assign values using '='.

    And more generally, when is each appropriate?

    ':' has a few different uses in QML. In your case, ':' performs a property binding. In contrast, '=' performs an assignment.

    See http://doc.qt.io/qt-5/qtqml-syntax-propertybinding.html for an explanation of property bindings.



  • Thank you both for your responses. I also appreciated the link; reading that helped a lot.

    I'll brush up on JavaScript.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.