Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. In QTreeView, it crashes when delete the last child
Forum Updated to NodeBB v4.3 + New Features

In QTreeView, it crashes when delete the last child

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 947 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.
  • jronaldJ Offline
    jronaldJ Offline
    jronald
    wrote on last edited by jronald
    #1

    Env

    Linux x86-64
    Qt 6.2.0

    Problem

    Settings for QTreeView:

    • QAbstractItemView::SelectRows
    • QAbstractItemView::SingleSelection

    When deleting a item that's not the last child, the current index is automatically move to the next sibling child.
    When deleting a item that is the last child, it crashes.

    If call selectionModel()->clearCurrentIndex() before deleting, it's successful to delete the last child. But the currentIndex is moved to the top left index, which is not friendly to users, any better way? e.g. move current index to next smartly (i.e. might be with a diffrent parent)

    1 Reply Last reply
    0
    • SGaistS SGaist

      Yes you should. The base class implementation does nothing. Only you know how to remove the data that makes your model.

      jronaldJ Offline
      jronaldJ Offline
      jronald
      wrote on last edited by jronald
      #9

      @Christian-Ehrlicher
      @SGaist

      Solved by adding the two lines of codes in MyModel::index, it works perfectly.

      QModelIndex MyModel::index(int row, int column, const QModelIndex & parent) const
      {
          if (!hasIndex(row, column, parent))
              return QModelIndex();
      
          ...
      }
      

      BTW, without the two lines of codes, everything works well except deleting the last child.
      I think it's a little hard to find out.

      Thanks

      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Please provide a minimal, compilable example so we can see how you delete your items and if you do something wrong.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        jronaldJ 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          Please provide a minimal, compilable example so we can see how you delete your items and if you do something wrong.

          jronaldJ Offline
          jronaldJ Offline
          jronald
          wrote on last edited by jronald
          #3

          @Christian-Ehrlicher
          When testing on "Simple Tree Model Example",
          After currentIndex is deleted, it is smart enough to find an existing item as currentIndex even with a different parent.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Hi,

            Are you using a custom model ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            jronaldJ 1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              Are you using a custom model ?

              jronaldJ Offline
              jronaldJ Offline
              jronald
              wrote on last edited by jronald
              #5

              @SGaist said in In QTreeView, it crashes when delete the last child:

              Are you using a custom model ?

              Yes, directly derived from QAbstractItemModel, everything else works well except this case.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #6

                Did you run it through the model tester ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                jronaldJ 1 Reply Last reply
                1
                • SGaistS SGaist

                  Did you run it through the model tester ?

                  jronaldJ Offline
                  jronaldJ Offline
                  jronald
                  wrote on last edited by jronald
                  #7

                  @SGaist said in In QTreeView, it crashes when delete the last child:

                  Did you run it through the model tester ?

                  No,maybe I should try it.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    Yes you should. The base class implementation does nothing. Only you know how to remove the data that makes your model.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    jronaldJ 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Yes you should. The base class implementation does nothing. Only you know how to remove the data that makes your model.

                      jronaldJ Offline
                      jronaldJ Offline
                      jronald
                      wrote on last edited by jronald
                      #9

                      @Christian-Ehrlicher
                      @SGaist

                      Solved by adding the two lines of codes in MyModel::index, it works perfectly.

                      QModelIndex MyModel::index(int row, int column, const QModelIndex & parent) const
                      {
                          if (!hasIndex(row, column, parent))
                              return QModelIndex();
                      
                          ...
                      }
                      

                      BTW, without the two lines of codes, everything works well except deleting the last child.
                      I think it's a little hard to find out.

                      Thanks

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        You should check your model with the model tester: https://wiki.qt.io/Model_Test

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        1

                        • Login

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