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. QTreeView with QAbstractItemModel collapses root node when selecting anything for the first time
Forum Updated to NodeBB v4.3 + New Features

QTreeView with QAbstractItemModel collapses root node when selecting anything for the first time

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

    In PyQt5, I have a QTreeView with a custom class extending QAbstractItemModel. I have a problem I can't seem to solve, or find the root cause, where only on the first time I select any node, the entire QTreeView collapses. When I expand the root node after, it no longer has that problem for the succeeding selections.

    At the beginning, my nodes have no children. I'm fetching the data / children from the server only if the node is expanded (using QAbstractItemView.expanded signal). I overrided QAbstractItemModel.insertRow and I call the recommended functions beginInsertRows and endInsertRows before and after I update the model data. I also emit the layoutToBeChanged and layoutChanged signals.

    I can't seem to figure out what causes that selection issue.

    This is my code for fetching data for nodes that are expanded:

    class FileTree(QTreeView):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            # Class for fetching data from server
            self.folders = Folders()
            self.setup_ui()
            self.populate()
            self.notify = NotificationDialog()
    
        def populate(self):
            ret, root_folders = self.folders.root()
            if ret:
                self.tree_model = TreeModel(root_folders)
                self.setModel(self.tree_model)
    
        def populate_folder_node(self, node, index):
            ret, children = self.folders.children(node.data['folder_id'])
            if ret:
                self.tree_model.removeRows(0, len(node.children), index)
                # Update view via model signal (Source: https://stackoverflow.com/a/51587327/5575610)
                self.tree_model.layoutAboutToBeChanged.emit()
                for i, data in enumerate(children):
                    self.tree_model.insertRow(Node(data), i, index)
                self.tree_model.layoutChanged.emit()
    
        def on_node_expanded(self, index: QModelIndex):
            if not index.isValid():
                return
            node = index.internalPointer()
            node.opened = True
            self.populate_folder_node(node, index)
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      martintan
      wrote on last edited by
      #2

      Resolved: It was a bug in my code

      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