Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Can't change current item in TreeView (v2) by mouse



  • I have implemented a simple TreeView (QCC2) which nests several levels. It renders properly, and I can navigate the tree up/down expand/collapse using the keyboard.

    But - Mouse clicks will not change the current model index, UNTIL....I click the expand icon on an item with children. After that it works fine. (Or until after I expand an item using the keyboard).

    This seems very strange...and I'm tempted to file a bug report, but is there something I'm missing?

    I have attached my code below - but not sure it help

    Component {
          id: delegateMenuItem
          Item {
              property int itemDepth: TreeView.depth
              property bool itemHasChildren: TreeView.hasChildren
              property bool itemIsExpanded: TreeView.isExpanded
              property var currentModelIndex: mytreeview.currentModelIndex
              property bool itemIsCurrent: (row === mytreeview.currentIndex.row) && (column === mytreeview.currentIndex.column)
    
              implicitWidth: Math.max(paneMenu.width -rectRightItemBackground.width/2,5)
              implicitHeight: Math.max(rectItemBackground.implicitHeight,5)
              Rectangle {
                  id: rectRightItemBackground
                  x: rectItemBackground.width -radius/2
                  anchors {
                      top: rectItemBackground.top
                      bottom: rectItemBackground.bottom
                  }
                  radius: rectItemBackground.height
                  width: radius
                  height: radius
                  color: rectItemBackground.color
              }
              Rectangle {
                  id: rectItemBackground
                  x: 0
                  color: itemIsCurrent ? gui.colorRGBString(GUIConstants.EColorElement_BackgroundSelected) : (TreeView.isExpanded ? gui.colorRGBString(GUIConstants.EColorElement_BackgroundPartialSelected) : gui.colorRGBString(GUIConstants.EColorElement_Background))
                  anchors.verticalCenter: parent.verticalCenter
                  anchors.left: parent.left;
                  implicitWidth: paneMenu.width -rectRightItemBackground.width/2
                  implicitHeight: itemHeightByDepth(itemDepth)
                  // Expand icon
                  Text {
                      id: indicator
                      x: itemDepth * 5
                      color: itemIsCurrent ? gui.colorRGBString(GUIConstants.EColorElement_TextSelected) : gui.colorRGBString(GUIConstants.EColorElement_Text)
                      text: itemHasChildren ? (itemIsExpanded ? "▼" : "▶") : ""
                      anchors.verticalCenter: parent.verticalCenter
                      leftPadding: 5
                      rightPadding: 5
    
                      TapHandler {
                          onTapped: {
                              console.log("In tap handler "+itemDepth+" "+name)
                              if (itemHasChildren)
                                  mytreeview.toggleExpanded(row)
                          }
                      }
    
                  }
                  // Top level icons
                  Image {
                      id: itemIcon
                      anchors.left: indicator.right
                      width: itemDepth === 0 ? 20 : 0
                      height: itemDepth === 0 ? 20 : 0
                      source: itemDepth !== 0 ? "" : "icons/dark/"+icon+".svg";  // Get icon string
                      anchors.verticalCenter: parent.verticalCenter
                      visible: itemDepth === 0
                  }
                  // Recolor the icon to match item
                  ColorOverlay {
                      anchors.fill: itemIcon
                      source: itemIcon
                      color: itemIsCurrent ? gui.colorRGBString(GUIConstants.EColorElement_TextSelected) : gui.colorRGBString(GUIConstants.EColorElement_Text)
                  }
    
                  // Item text
                  Text {
                      anchors.left: itemIcon.right
                      width: rectItemBackground.width
                      verticalAlignment: Text.AlignVCenter
                      height: parent.height
                      id: itemText
                      text: qsTr(name) //+" C:"+currentModelIndex+(itemIsCurrent?"Y":"N")+" S:"+(itemIsSelected?"Y":"N")
                      font.pixelSize: fontSizeByDepth(itemDepth)
                      leftPadding: 10
                      elide: Text.ElideRight
                      color: itemIsCurrent ? gui.colorRGBString(GUIConstants.EColorElement_TextSelected) : gui.colorRGBString(GUIConstants.EColorElement_Text)
                  }  // Tezt
              }  // Rectangle
          }  // Item
      }  // Component
    
    
      Rectangle {
          id: paneMenu
          anchors.fill: parent
          color: gui.colorRGBString(GUIConstants.EColorElement_Background)
          TreeViewTemplate {
              id: mytreeview
              anchors.fill: parent
              model: treemodel
              delegate: delegateMenuItem
              currentModelIndex: viewIndex(0,0)
              currentIndex: viewIndex(0,0)
              focus: true
              onCurrentModelIndexChanged: {
                  console.log("In item change"+currentModelIndex);
    
              }
          }
      }
    

Log in to reply