QAbstractItemModel: annoying branch line in QTreeView

此生再无相见时 提交于 2019-12-12 02:12:53

问题


I've made a minimalistic subclass of QAbstractItemModel. It works, but in QTreeView there is a odd branch line that should not appear here (because there is no more items after [Sensor arrays] section). Cannot you tell me, what have I done wrong and how to fix it?

Here is my code:

import os, sys

from PyQt5 import QtCore, QtGui, QtWidgets

import treelib


class BlockHierarchyNode(object):
    def __init__(self, icon_path=None, name="", description="", is_category=False):
        if icon_path is not None:
            self._icon = QtGui.QPixmap(icon_path)
            if self._icon.size() == QtCore.QSize():  # zero size (loading error)
                assert False

        else:
            self._icon = None

        self._is_category = bool(is_category)
        self._name = str(name)
        self._description = str(description)

    @property
    def icon(self):
        return self._icon

    @property
    def is_category(self):
        return self._is_category

    @property
    def name(self):
        return self._name

    @property
    def description(self):
        return self._description

    def __str__(self):
        if self.is_category:
            return "[%s]" % self.name
        else:
            return self.name


class AvailableBlocksModel(QtCore.QAbstractItemModel):
    def __init__(self, data_tree, parent=None):
        super(AvailableBlocksModel, self).__init__(parent)

        self._tree = data_tree
        assert isinstance(data_tree, treelib.Tree)

    def columnCount(self, parent_index):
        if parent_index.isValid():
            return 1
        else:
            return 1

    def rowCount(self, parent_index):
        if parent_index.isValid():
            return len(self._tree.children(parent_index.internalPointer().identifier))
        else:  # root node
            return 1

    def data(self, index, role):

        if not index.isValid():
            return None

        if role == QtCore.Qt.DisplayRole:
            return str(index.internalPointer().data)
        elif role == QtCore.Qt.DecorationRole:
            icon = index.internalPointer().data.icon
            if icon is not None:
                return icon

        return None

    def index(self, row, column, parent=QtCore.QModelIndex()):
        if not self.hasIndex(row, column, parent):
            return QtCore.QModelIndex()
        if parent.isValid():
            parent_node = parent.internalPointer()
            children = self._tree.children(parent_node.identifier)
            return self.createIndex(row, column, children[row])
        else:
            return self.createIndex(row, column, self._tree.nodes[self._tree.root])

    def parent(self, index):
        if not index.isValid():
            return QtCore.QModelIndex()

        child_item = index.internalPointer()
        parent_item = self._tree.parent(child_item.identifier)

        if parent_item is None:
            return QtCore.QModelIndex()

        parent_row = 0
        siblings = self._tree.siblings(parent_item.identifier)
        for i, elm in enumerate(siblings):
            if elm == parent_item:
                parent_row = i

        return self.createIndex(parent_row, 0, parent_item)


class MegaTree(treelib.Tree):
    def add_block_node(self, data_block, identifier=None, parent_node=None):
        id = identifier if identifier is not None else data_block.name
        if parent_node is None:
            return self.create_node(str(data_block), id, data=data_block)
        else:
            return self.create_node(str(data_block), id, data=data_block, parent=parent_node.identifier)

    @staticmethod
    def create_demo_tree():
        tree = MegaTree()
        root_data = BlockHierarchyNode(name="All blocks", is_category=True)
        root_node = tree.add_block_node(root_data, "root")  # root node


        waveforms_data = BlockHierarchyNode(name="Waveforms", is_category=True)
        waveforms_node = tree.add_block_node(waveforms_data, parent_node=root_node)

        rect_waveform_data = BlockHierarchyNode(name="Rectangular waveform", is_category=False)
        rect_waveform_node = tree.add_block_node(rect_waveform_data, parent_node=waveforms_node)

        lfm_waveform_data = BlockHierarchyNode(name="LFM waveform", is_category=False)
        lfm_waveform_node = tree.add_block_node(lfm_waveform_data, parent_node=waveforms_node)

        phasecoded_waveform_data = BlockHierarchyNode(name="Phase coded waveform", is_category=False)
        phasecoded_waveform_node = tree.add_block_node(phasecoded_waveform_data, parent_node=waveforms_node)


        sensor_array_data = BlockHierarchyNode(name="Sensor arrays", is_category=True)
        sensor_array_node = tree.add_block_node(sensor_array_data, parent_node=root_node)

        ura_data = BlockHierarchyNode(name="URA", is_category=False)
        ura_node = tree.add_block_node(ura_data, parent_node=sensor_array_node)

        ula_data = BlockHierarchyNode(name="ULA", is_category=False)
        ula_node = tree.add_block_node(ula_data, parent_node=sensor_array_node)

        return tree


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)

    model = AvailableBlocksModel(MegaTree.create_demo_tree())

    view = QtWidgets.QTreeView()

    view.setModel(model)
    view.expandAll()
    view.show()

    sys.exit(app.exec_())

回答1:


Oh, I've just forgotten that the siblings method of Tree class of treelib gives strictly the siblings of a node (without the node itself in the list). So, in parent method its row was always 0. = (

Now the method is here:

def parent(self, index):
    if not index.isValid():
        return QtCore.QModelIndex()

    child_item = index.internalPointer()
    parent_item = self._tree.parent(child_item.identifier)

    if parent_item is None:
        return QtCore.QModelIndex()

    if parent_item.is_root():
        parent_row = 0
    else:
        siblings = self._tree.children(parent_item.bpointer)
        for i, elm in enumerate(siblings):
            if elm == parent_item:
                parent_row = i


来源:https://stackoverflow.com/questions/42565139/qabstractitemmodel-annoying-branch-line-in-qtreeview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!