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. How to update QTreeView when reordering siblings in a custom Tree model
Forum Updated to NodeBB v4.3 + New Features

How to update QTreeView when reordering siblings in a custom Tree model

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 622 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.
  • O Offline
    O Offline
    OpenGL
    wrote on last edited by
    #1

    I have a QTreeView that displays my custom model.
    It works, but now I need to reorder the children of a item in the tree.

    I tried to do this in the modell, but it crashes my program

    In this code, item is the current item that should change order among its siblings

    beginMoveRows(item->parentItem()->index(),  1,   item->parentItem()->childCount(), item->parentItem()->index(), 1);
    // re order the children
    endMoveRows();
    

    Any advice on how to do this?

    Or is there a way of reloading the entire model?

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #2

      Are you only moving one item? I think it should be

      beginMoveRows(parent's index,  item's curent row, item's current row again, parent's index again, the row item is moving to);
      

      Take a detail look at beginMoveRows's 3rd and 4th example.

      For reloading, I think that should be beginResetModel() and endResetModel()

      1 Reply Last reply
      2
      • O Offline
        O Offline
        OpenGL
        wrote on last edited by
        #3

        Yes, only moving one item, but after reading the examples I think it will be trick yo use the beginMoveRows method, since I'm moving one item by calling std::sort on all the siblings (including the moved item, they are in a QList)
        So I don't really know where the item will end up before moving them.

        I can of course try to find the new position before sorting, but that feels like double-work.
        I guess a call to beginResetModel would be the easiest way (but slower?)

        Or is there any better ways of doing this?

        B 1 Reply Last reply
        0
        • O OpenGL

          Yes, only moving one item, but after reading the examples I think it will be trick yo use the beginMoveRows method, since I'm moving one item by calling std::sort on all the siblings (including the moved item, they are in a QList)
          So I don't really know where the item will end up before moving them.

          I can of course try to find the new position before sorting, but that feels like double-work.
          I guess a call to beginResetModel would be the easiest way (but slower?)

          Or is there any better ways of doing this?

          B Offline
          B Offline
          Bonnie
          wrote on last edited by
          #4

          @OpenGL
          If it is about sorting I think you can take a look at the Qt source code about how its treemodel handles an item's children sorting.

          https://code.woboq.org/qt5/qtbase/src/widgets/itemviews/qtreewidget.cpp.html#_ZN15QTreeWidgetItem12sortChildrenEiN2Qt9SortOrderEb

          emit model->layoutAboutToBeChanged({}, QAbstractItemModel::VerticalSortHint);
          //...do the sorting...update the persistent index list if there's any
          emit model->layoutChanged({}, QAbstractItemModel::VerticalSortHint);
          

          As I can see it just emit layoutAboutToBeChanged and layoutChanged signals without specifying any index.

          1 Reply Last reply
          0
          • O Offline
            O Offline
            OpenGL
            wrote on last edited by
            #5

            I solved it by calculating the new position for the item.

            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