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

Odd syntax errors using conditional in list property binding

XMLWordPrintable

      Assuming an item, ExampleItem has a list-type (e.g. backed by QQmlListProperty) property, called "things", we can, in qml, bind it to a list of objects:

      ExampleItem {
          things: [
              Thing { name: "name1" },
              Thing { name: "name2" }
          ]
      }
      

      However, if we want to conditionally assign different lists based on another property, it doesn't appear to work:

      ExampleItem {
          property bool someCondition
          things: someCondition ? [
              Thing { name: "name1" },
              Thing { name: "name2" }
          ] : [
              Thing { name: "name3" }
          ]
      }
      
       Expected token `,'
       Expected token `,'
       Expected token `,'
      
      

      At first I thought that maybe you couldn't use a conditional in a list property binding. However, the following does work:

       

      ExampleItem {
          property bool someCondition
          property var thing1: Thing { name: "name1" }
          property var thing2: Thing { name: "name2" }
          property var thing3: Thing { name: "name3" }
          things: someCondition ? [ thing1 , thing2 ] : [ thing3 ]
      }
      

      I can only assume that conditional bindings with lists of object for list properties do work, but that the QML/Javascript parser doesn't handle the syntax properly if you try to declare the objects inline.

       

      Note that the example with "property var thing1" isn't quite the same as was intended, as it always creates all possible list contents, instead of just creating those which are currently in the list (thing1 and thing2 are created even when someCondition is false).

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

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            THall Tom Hall
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes