Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-104324

Simplify QML optional chaining operator usage with bool properties

XMLWordPrintable

    • All

      Qt 6.2 has a nice addition being able use optional chaining operator in QML bindings:

       

       

      Item
      {
        property Item item
        visible: item?.someBooleanProperty
      }
      

       

       

      This kind of a use scenario is common from my experience with our large QML app.

      However, that could be made simpler, by allowing setting 'undefined' as false to boolean properties. Now this causes "Error: Cannot assign [undefined] to bool" and if the assignment is done in a function call instead of in a binding, the error may cause important code to be skipped. It's tedious and easy to forget to convert it to boolean in every use case:

       

      visible: Boolean(item?.someBooleanProperty)

      or

      visible: !!(item?.someBooleanProperty)

      or

      visible: item?.someBooleanProperty ?? false

       

      Probably you can yourself register a converter from undefined to boolean properties in your app, but I think this should be enabled by default. What do you think?

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            qtqmlteam Qt Qml Team User
            -mtuokko- Mikko Tuokko
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes