Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Qt 6 TreeView and selection
Forum Updated to NodeBB v4.3 + New Features

Qt 6 TreeView and selection

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 3 Posters 466 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bob64
    wrote last edited by
    #1

    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?

    Z 1 Reply Last reply
    0
    • B Offline
      B Offline
      Bob64
      wrote last edited by Bob64
      #4

      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 of TreeViewDelegate is coloured according to a property highlighted that is defined in TreeViewDelegate. This highlighted property in turn becomes true if the delegate's selected or current 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?

      GrecKoG 1 Reply Last reply
      0
      • B Bob64

        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?

        Z Offline
        Z Offline
        zvoopz
        wrote last edited by zvoopz
        #2

        @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

        B 1 Reply Last reply
        0
        • Z zvoopz

          @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

          B Offline
          B Offline
          Bob64
          wrote last edited by
          #3

          @zvoopz It is my understanding that all properties exposed to QML have an associated on<PropertyName>Changed signal to which one may bind. Since selectedIndexes is a property of the ItemSelectionModel, 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, of ItemSelectionModel and this is not being triggered either.

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Bob64
            wrote last edited by Bob64
            #4

            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 of TreeViewDelegate is coloured according to a property highlighted that is defined in TreeViewDelegate. This highlighted property in turn becomes true if the delegate's selected or current 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?

            GrecKoG 1 Reply Last reply
            0
            • B Bob64

              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 of TreeViewDelegate is coloured according to a property highlighted that is defined in TreeViewDelegate. This highlighted property in turn becomes true if the delegate's selected or current 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?

              GrecKoG Offline
              GrecKoG Offline
              GrecKo
              Qt Champions 2018
              wrote last edited by
              #5

              @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 to setCurrentIndex then yes.
              Note that TreeView (as inheriting from TableView) 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

              B 1 Reply Last reply
              1
              • GrecKoG GrecKo

                @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 to setCurrentIndex then yes.
                Note that TreeView (as inheriting from TableView) 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

                B Offline
                B Offline
                Bob64
                wrote last edited by
                #6

                @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.

                1 Reply Last reply
                0
                • B Bob64 has marked this topic as solved
                • B Bob64 has marked this topic as solved

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved