Qt 6 TreeView and selection
-
I am currently trying to port a Qt 5 QML application to Qt 6.8. This has mostly been straightforward apart form the tree view.
I expected to have to do some work, but there is one aspect that looked like it would port over fairly straightforwardly and I can't get it to work. This involves getting notification of the current selected index.
If I set
selectionModel: ItemSelectionModel {}
, it clearly makes a difference as I see the effect of selecting rows.However, if I try to attach to any signals from the model, I see nothing being triggered.
selectionModel: ItemSelectionModel { onSelectedIndexesChanged: { if (selectedIndexes.length > 0) { console.log("Selected row:", selectedIndexes[0].row, "Column:", selectedIndexes[0].column) } } onSelectionChanged: { console.log("Selection changed") } }
I have reproduced the (lack of) behaviour that I see in my own application using the "Table of Contents" example from Qt. Just add the above code to the selection model that is already assigned in the example.
Generally, I find the documentation for
ItemSelectionModel
to be a bit sparse so it is possible that I have completely misunderstood something about it.Am I missing some other way to be notified about selected indices?
-
Since my original post I have done some hunting around in Qt source and examining it under the debugger. It looks like the effect of clicking on a tree branch is to make it "selected" in Qt 5. In Qt 6, it only seems to make it "current". The default
background
ofTreeViewDelegate
is coloured according to a propertyhighlighted
that is defined inTreeViewDelegate
. Thishighlighted
property in turn becomes true if the delegate'sselected
orcurrent
flag is true.I think I might have a broader question now of whether "selected" or "current" is the correct behaviour for my own situation and there isn't a huge amount of documentation around the concepts. I might ask that on the general forum though.
A QML specific question is that if I did want to make a branch selected, then is it simply a case of adding a tap handler in the delegate somewhere and calling
setCurrentIndex
on the selection model, using the index of the current delegate? -
I am currently trying to port a Qt 5 QML application to Qt 6.8. This has mostly been straightforward apart form the tree view.
I expected to have to do some work, but there is one aspect that looked like it would port over fairly straightforwardly and I can't get it to work. This involves getting notification of the current selected index.
If I set
selectionModel: ItemSelectionModel {}
, it clearly makes a difference as I see the effect of selecting rows.However, if I try to attach to any signals from the model, I see nothing being triggered.
selectionModel: ItemSelectionModel { onSelectedIndexesChanged: { if (selectedIndexes.length > 0) { console.log("Selected row:", selectedIndexes[0].row, "Column:", selectedIndexes[0].column) } } onSelectionChanged: { console.log("Selection changed") } }
I have reproduced the (lack of) behaviour that I see in my own application using the "Table of Contents" example from Qt. Just add the above code to the selection model that is already assigned in the example.
Generally, I find the documentation for
ItemSelectionModel
to be a bit sparse so it is possible that I have completely misunderstood something about it.Am I missing some other way to be notified about selected indices?
-
@Bob64 said in Qt 6 TreeView and selection:
onSelectedIndexesChanged:
I may suggest that this is not a proper signal/function whatever
Just put a console.log outside if-statement to see if it triggers@zvoopz It is my understanding that all properties exposed to QML have an associated
on<PropertyName>Changed
signal to which one may bind. SinceselectedIndexes
is a property of theItemSelectionModel
, there ought to be a corresponding signal,onSelectedIndexesChanged
.See this documentation.
This can be thought of as an implicitly provided signal, but note that there is also an explicitly documented signal,
selectionChanged
, ofItemSelectionModel
and this is not being triggered either. -
Since my original post I have done some hunting around in Qt source and examining it under the debugger. It looks like the effect of clicking on a tree branch is to make it "selected" in Qt 5. In Qt 6, it only seems to make it "current". The default
background
ofTreeViewDelegate
is coloured according to a propertyhighlighted
that is defined inTreeViewDelegate
. Thishighlighted
property in turn becomes true if the delegate'sselected
orcurrent
flag is true.I think I might have a broader question now of whether "selected" or "current" is the correct behaviour for my own situation and there isn't a huge amount of documentation around the concepts. I might ask that on the general forum though.
A QML specific question is that if I did want to make a branch selected, then is it simply a case of adding a tap handler in the delegate somewhere and calling
setCurrentIndex
on the selection model, using the index of the current delegate? -
Since my original post I have done some hunting around in Qt source and examining it under the debugger. It looks like the effect of clicking on a tree branch is to make it "selected" in Qt 5. In Qt 6, it only seems to make it "current". The default
background
ofTreeViewDelegate
is coloured according to a propertyhighlighted
that is defined inTreeViewDelegate
. Thishighlighted
property in turn becomes true if the delegate'sselected
orcurrent
flag is true.I think I might have a broader question now of whether "selected" or "current" is the correct behaviour for my own situation and there isn't a huge amount of documentation around the concepts. I might ask that on the general forum though.
A QML specific question is that if I did want to make a branch selected, then is it simply a case of adding a tap handler in the delegate somewhere and calling
setCurrentIndex
on the selection model, using the index of the current delegate?@Bob64 said in Qt 6 TreeView and selection:
A QML specific question is that if I did want to make a branch selected, then is it simply a case of adding a tap handler in the delegate somewhere and calling setCurrentIndex on the selection model, using the index of the current delegate?
If you pass the
Select
flag tosetCurrentIndex
then yes.
Note thatTreeView
(as inheriting fromTableView
) use an internal proxy model so you'd have to map the index of the delegate to get an index in the actual underlying model : https://doc.qt.io/qt-6/qml-qtquick-tableview.html#index-method -
@Bob64 said in Qt 6 TreeView and selection:
A QML specific question is that if I did want to make a branch selected, then is it simply a case of adding a tap handler in the delegate somewhere and calling setCurrentIndex on the selection model, using the index of the current delegate?
If you pass the
Select
flag tosetCurrentIndex
then yes.
Note thatTreeView
(as inheriting fromTableView
) use an internal proxy model so you'd have to map the index of the delegate to get an index in the actual underlying model : https://doc.qt.io/qt-6/qml-qtquick-tableview.html#index-method@GrecKo Thanks for the tip. In the Qt 5 tree view, the index was provided directly to the delegate but I see that it has to be derived from the row and column that are provided to the delegate in Qt 6. So I need to make sure I use the provided
index()
method on the tree to do this, rather than going directly to the model. -
-