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. Why is custom tree item data undefined when tree node collapses?
Forum Updated to NodeBB v4.3 + New Features

Why is custom tree item data undefined when tree node collapses?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
8 Posts 2 Posters 758 Views 1 Watching
  • 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.
  • M Offline
    M Offline
    mirro
    wrote on last edited by mirro
    #1

    I'm using QAbstractItemModel.

    w5UIN4.jpg

    w5U5EF.jpg

    w5Uo4J.jpg

    The reference sample.
    https://github.com/hvoigt/qt-qml-treeview/blob/master/TreeDelegate.qml

    1 Reply Last reply
    0
    • M mirro

      @sierdzio Oh, Do you mean that the QML calls the C++ QAbstractItemModel methods for processing?

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #8

      @mirro said in Why is custom tree item data undefined when tree node collapses?:

      @sierdzio Oh, Do you mean that the QML calls the C++ QAbstractItemModel methods for processing?

      I don't know your code structure. My assumption was that you are using QAbstractItemModel and your radio button state is controlled by CheckStateRole.

      If, on the other hand, you are using some different mechanism, you need to adapt to that. But definitely, regardless of the mechanism - changing a state (any kind of state - item name, check state, color etc.) is a job of the model (or underlying data), not the view.

      (Z(:^

      1 Reply Last reply
      0
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #2

        When delegate stops being shown (moves off screen, is clipped or collapsed) QML engine usually destroys it. And on model side, collapsed indexes are invalid so data() will return empty QVariant.

        Do this to get rid of the warning:

        text: styleData === undefined? "" : styleData.value.name
        

        (and please, please: paste text not pictures where you want to show code! It's much easier for me to then copy-paste your code and show tweaks or corrections)

        (Z(:^

        M 1 Reply Last reply
        2
        • sierdzioS sierdzio

          When delegate stops being shown (moves off screen, is clipped or collapsed) QML engine usually destroys it. And on model side, collapsed indexes are invalid so data() will return empty QVariant.

          Do this to get rid of the warning:

          text: styleData === undefined? "" : styleData.value.name
          

          (and please, please: paste text not pictures where you want to show code! It's much easier for me to then copy-paste your code and show tweaks or corrections)

          M Offline
          M Offline
          mirro
          wrote on last edited by
          #3

          @sierdzio Thank you for your question and answer.

          What method can I use to traverse the children under the selected node in QML?
          Do I need to add methods to the QAbstractItemModel in C++ ?

          MouseArea{
              OnPressed:{
                       //...
                       if(styleData.depth === 1)
                       {
                                var curIndex = treeView.indexAt(mouse.x,mouse.y)
                       }
              }
          }
          
          sierdzioS 1 Reply Last reply
          0
          • M mirro

            @sierdzio Thank you for your question and answer.

            What method can I use to traverse the children under the selected node in QML?
            Do I need to add methods to the QAbstractItemModel in C++ ?

            MouseArea{
                OnPressed:{
                         //...
                         if(styleData.depth === 1)
                         {
                                  var curIndex = treeView.indexAt(mouse.x,mouse.y)
                         }
                }
            }
            
            sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #4

            @mirro said in Why is custom tree item data undefined when tree node collapses?:

            @sierdzio Thank you for your question and answer.

            What method can I use to traverse the children under the selected node in QML?

            Don't do it unless you have to - it's too error prone with delegates coming in and out of existence all the time.

            Do I need to add methods to the QAbstractItemModel in C++ ?

            MouseArea{
                OnPressed:{
                         //...
                         if(styleData.depth === 1)
                         {
                                  var curIndex = treeView.indexAt(mouse.x,mouse.y)
                         }
                }
            }
            

            Move your MouseArea into the delegate - this way you don't have to find which index and item is affected, because pressed signal will be emitted and handled directly in the item which was clicked.

            (Z(:^

            M 1 Reply Last reply
            0
            • sierdzioS sierdzio

              @mirro said in Why is custom tree item data undefined when tree node collapses?:

              @sierdzio Thank you for your question and answer.

              What method can I use to traverse the children under the selected node in QML?

              Don't do it unless you have to - it's too error prone with delegates coming in and out of existence all the time.

              Do I need to add methods to the QAbstractItemModel in C++ ?

              MouseArea{
                  OnPressed:{
                           //...
                           if(styleData.depth === 1)
                           {
                                    var curIndex = treeView.indexAt(mouse.x,mouse.y)
                           }
                  }
              }
              

              Move your MouseArea into the delegate - this way you don't have to find which index and item is affected, because pressed signal will be emitted and handled directly in the item which was clicked.

              M Offline
              M Offline
              mirro
              wrote on last edited by mirro
              #5

              @sierdzio Hi~So how do I make it so that when I check the main node, all the children are checked together?

              wHpiC9.jpg

              1 Reply Last reply
              0
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #6

                You need to do it in the model, for sure. This stuff belongs to setData() method in the model.

                (Z(:^

                M 1 Reply Last reply
                0
                • sierdzioS sierdzio

                  You need to do it in the model, for sure. This stuff belongs to setData() method in the model.

                  M Offline
                  M Offline
                  mirro
                  wrote on last edited by
                  #7

                  @sierdzio Oh, Do you mean that the QML calls the C++ QAbstractItemModel methods for processing?

                  sierdzioS 1 Reply Last reply
                  0
                  • M mirro

                    @sierdzio Oh, Do you mean that the QML calls the C++ QAbstractItemModel methods for processing?

                    sierdzioS Offline
                    sierdzioS Offline
                    sierdzio
                    Moderators
                    wrote on last edited by
                    #8

                    @mirro said in Why is custom tree item data undefined when tree node collapses?:

                    @sierdzio Oh, Do you mean that the QML calls the C++ QAbstractItemModel methods for processing?

                    I don't know your code structure. My assumption was that you are using QAbstractItemModel and your radio button state is controlled by CheckStateRole.

                    If, on the other hand, you are using some different mechanism, you need to adapt to that. But definitely, regardless of the mechanism - changing a state (any kind of state - item name, check state, color etc.) is a job of the model (or underlying data), not the view.

                    (Z(:^

                    1 Reply Last reply
                    0

                    • Login

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