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

TreeViewDelegate: selection via onClicked does not work

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Invalid
    • Icon: P2: Important P2: Important
    • None
    • 6.4.0
    • Quick: Controls 2
    • None
    • All

      When using TreeView with TreeViewDelegate, selecting cells in onClicked handler does not work as expected. Cell is selected at first, but gets unselected right after.

      Demo code (full project here, branch treeviewdelegate-selection):

      import QtQuick
      import QtQuick.Window
      import QtQuick.Controls
      
      Window {
      	id: root
      	visible: true
      
      	width: 640
      	height: 480
      
      	property int msgNr: 0
      
      	TreeView {
      		id: tree_view
      
      		anchors.fill: parent
      
      		selectionModel: ItemSelectionModel {
      			id: selection_model
      		}
      
      		model: _tree_proxy_model
      
      		// this works as expected
      		//delegate: ItemDelegate {
      		//	required property bool selected
      		//	required property bool current
      		//
      		// this does not work as expected (selected item immediately gets unselected)
      		delegate: TreeViewDelegate {
      			id: delegate
      
      			onClicked: {
      				console.log(`${++msgNr}: ${model.display}: onClicked`)
      
      				// OMG why column is the first argument?
      				// This is different from QAbstractItemModel::index(int row, int column, ...)
      				// This also breaks Qt 6.3 code which used TreeView's modelIndex method which accepted (row, column)
      				let idx = tree_view.modelIndex(column, row)
      
      				selection_model.select(idx, ItemSelectionModel.Toggle)
      
      				// commenting this makes no difference
      				selection_model.setCurrentIndex(idx, ItemSelectionModel.Current)
      			}
      
      			onSelectedChanged: {
      				console.log(`${++msgNr}: ${model.display}: onSelectedChanged, selected: ${selected}`)
      			}
      
      			onCurrentChanged: {
      				console.log(`${++msgNr}: ${model.display}: onCurrentChanged, current: ${current}`)
      			}
      
      			contentItem: Text {
      				text: model.display
      				color: delegate.selected ? "white" : "black"
      			}
      
      			background: Rectangle {
      				color: delegate.selected ? "navy" : "white"
      				border.color: delegate.current ? "navy" : "transparent"
      				border.width: delegate.current ? 2 : 0
      			}
      		}
      	}
      }
      

      I get the following output when clicking on the cell (0,0):

      qml: 1: Connection Editing Mode: onClicked
      qml: 2: Connection Editing Mode: onSelectedChanged, selected: 1
      qml: 3: Connection Editing Mode: onSelectedChanged, selected: 0
      qml: 4: Connection Editing Mode: onCurrentChanged, current: 1
      

      The third message is the result of calling clearSelection in QQuickTapHandler::tapped handler in QQuickTableViewPrivate::init (qtdeclarative\src\quick\items\qquicktableview.cpp#4053).

      When I replace TreeViewDelegate with ItemDelegate, everything works as expected (but of course no tree indicators and unable to expand notes). I would like to use TreeViewDelegate so I won't have to implement indicators, offsets and nodes expansion on my own.

      I tried different ways (select with and without setCurrentIndex, different commands Select, SelectCurrent, ToggleCurrent - no success with TreeViewDelegate).

      I attached two gifs: expected behavior with ItemDelegate and wrong behavior with TreeViewDelegate.

        1. 1-treeview-delegate-wrong.gif
          207 kB
          Vladimir Serdyuk
        2. 2-itemdelegate-ok.gif
          242 kB
          Vladimir Serdyuk
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            richard Richard Moe Gustavsen
            vserdyuk Vladimir Serdyuk
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes