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

No way of identifying elements created by Repeater

    XMLWordPrintable

Details

    • c1a87921c8a85d5ecee9aba3b3578ae8d042c28a, c527a1ee371c4758776217e7d16c75e20fe6f5aa

    Description

      Consider the code below:

      ExclusiveGroup {
          model: VisualItemModel {
              Button { id: button1; text: "Button A"; property alias selected: button1.checked; checkable: true } // see QTBUG-15139
              Button { id: button2; text: "Button B"; property alias selected: button2.checked; checkable: true }
              Button { id: button3; text: "Button C"; property alias selected: button3.checked; checkable: true }
          }
      }
      

      ExclusiveGroup.qml

      import QtQuick 1.0
      
      Item {
          property alias model: repeater.model
          property Item selectedItem: null
          Row {
              Repeater { id: repeater }
      
              onChildrenChanged: {
                  var _child = children[children.length-1];    // PROBLEM IS HERE
                  _child.selectedChanged.connect(_child, handleSelectionChanged);
                  if(_child.selected) {
                      if(selectedItem) {
                          selectedItem.selected = false;
                      }
                      selectedItem = _child;
                  }
              }
      
              Component.onCompleted: {
                  if(!selectedItem) {
                      for(var i = 0; i < children.length; i++) {
                          var _child = children[i];
                          if(_child != repeater) {
                              selectedItem = _child;
                              selectedItem.selected = true;
                              break;
                          }
                      }
                  }
              }
      
              property bool selectionChanging: false
      
              function handleSelectionChanged() {
                  print("Selection changed! Item: " + this + " selected: " + this.selected);
                  if(this.selected && this != selectedItem) {
                      if(selectedItem) {
                          selectionChanging = true;
                          selectedItem.selected = false;
                          selectionChanging = false
                      }
      
                      selectedItem = this;
                  } else if(!selectionChanging && selectedItem == this && !this.selected) {    // Click was on already selected item
                      this.selected = true;   // Reselect it
                  }
              }
          }
      }
      

      Here when an element is created by the Repeater we want to connect the selectedChanged signal of the element to the handleSelectionChanged function. The problem is that there's no way of reliably getting to the element just created. The code above relies on the onChildrenChanged slot and assumes the new item is the last in the list of children, but this is unreliable (the new item is not necessarily the last (right?), and the onChildrenChanged is called also when children are removed). Something like a Repeater::itemCreated(item) signal is needed (and Repeater::itemDestroyed(item)?).

      Attachments

        Issue Links

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

          Activity

            People

              bealam Bea Lam (closed Nokia identity) (Inactive)
              mathiasm Mathias Malmqvist
              Votes:
              4 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes