子表上的QAbstractItemModel标头

时间:2018-10-16 12:18:03

标签: python python-3.x pyqt pyqt5

我创建了自己的QAbstractItemModel,它具有树状结构和几个子表,如下所示:

group1
  - order1
     - item1
     - item2
  - order2
     - item3
group2
  - order3
     - item4
     - item5
     - item6

我的目标是要有树和表格视图。当我在树形视图中单击一个订单时,我想显示一个表,其中包含表中的所有项目(及其属性)。到目前为止,我已经开始工作了。

现在,我显然需要表视图的标题,这在这里有些卡住。我尝试从this问题开始实施解决方案,但无法正常工作。这是我到目前为止所得到的:

import sys
from PyQt5 import QtCore, QtWidgets


class Node(object):

    def __init__(self, parent=None, item_count=0):
        self.parent = parent
        self.children = []
        self.item_count = item_count
        if self.parent is not None:
            self.parent.add_child(self)

    def add_child(self, child):
        self.children.append(child)
        child.parent = self

    @property
    def row(self):
        if self.parent is None:
            return 0
        return self.parent.children.index(self)

    @property
    def child_item_count(self):
        if len(self.children):
            return self.children[0].item_count
        return 1

    @property
    def child_count(self):
        return len(self.children)

    def child(self, row):
        try:
            return self.children[row]
        except IndexError:
            return None

    def header_data(self):
        return []


class ItemNode(Node):

    def __init__(self, parent=None, name='', description='', price=''):
        super(ItemNode, self).__init__(parent=parent, item_count=3)
        self.name = name
        self.description = description
        self.price = price

    def data(self, column):
        if column == 0:  return self.name
        if column == 1:  return self.description
        if column == 2:  return self.price
        return None


class OrderNode(Node):

    def __init__(self, parent=None, order_id='', order_date=''):
        super(OrderNode, self).__init__(parent=parent, item_count=2)
        self.order_id = order_id
        self.order_date = order_date

    def data(self, column):
        if column == 0:  return self.order_id
        if column == 1:  return self.order_date
        return None

    def header_data(self):
        return ['Name', 'Description', 'Price']


class GroupNode(Node):

    def __init__(self, parent=None, name='', description=''):
        super(GroupNode, self).__init__(parent=parent, item_count=2)
        self.name = name
        self.description = description

    def data(self, column):
        if column == 0: return self.name
        if column == 1: return self.description
        return None


class ItemModel(QtCore.QAbstractItemModel):

    def __init__(self, root=None):
        if root is None:
            root = Node()
        self.root = root
        super(ItemModel, self).__init__()

    def rowCount(self, parent=None, *args, **kwargs):
        node = self.get_node(parent)
        return node.child_count

    def columnCount(self, parent=None, *args, **kwargs):
        node = self.get_node(parent)
        return node.child_item_count

    def index(self, row, column, parent=None, *args, **kwargs):
        node = self.get_node(parent)
        return self.createIndex(row, column, node.child(row))

    def parent(self, index=None):
        node = self.get_node(index)
        if node.parent is None:
            return QtCore.QModelIndex()
        return self.createIndex(node.parent.row, 0, node.parent)

    def data(self, index, role=QtCore.Qt.DisplayRole):
        node = self.get_node(index)
        if role == QtCore.Qt.DisplayRole:
            return node.data(index.column())
        if role == QtCore.Qt.UserRole + 1:  # horizontal header role
            return node.header_data()

    def get_node(self, index):
        if index and index.isValid():
            node = index.internalPointer()
            if node:
                return node
        return self.root

    def is_order(self, index):
        node = self.get_node(index)
        return isinstance(node, OrderNode)


class ProxyItemModel(QtCore.QSortFilterProxyModel):

    horizontal_header_role = QtCore.Qt.UserRole + 1

    def __init__(self):
        self.root_index = QtCore.QModelIndex()
        super(ProxyItemModel, self).__init__()

    def set_root_index(self, index):
        self.root_index = self.mapToSource(index)
        if self.sourceModel() and self.root_index.internalPointer() is not None:
            self.headerDataChanged.emit(
                QtCore.Qt.Horizontal, 0, self.sourceModel().columnCount(self.root_index)
            )

    def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole):
        if self.sourceModel() and self.root_index.isValid():
            if orientation == QtCore.Qt.Horizontal:
                role = self.horizontal_header_role
            header_list = self.sourceModel().data(self.root_index, role)
            if header_list and 0 <= section < len(header_list):
                return header_list[section]
        return super(ProxyItemModel, self).headerData(section, orientation, role)

    def is_order(self, index):
        source_index = self.mapToSource(index)
        return self.sourceModel().is_order(source_index)


class MainWindow(QtWidgets.QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        self.central_widget = QtWidgets.QWidget(self)
        self.setCentralWidget(self.central_widget)
        central_layout = QtWidgets.QVBoxLayout(self)
        self.central_widget.setLayout(central_layout)

        self.treeView = QtWidgets.QTreeView(self)
        self.tableView = QtWidgets.QTableView(self)
        central_layout.addWidget(self.treeView)
        central_layout.addWidget(self.tableView)

        self.data_model = ItemModel()
        self.set_up_data_model()
        self.proxy_model = ProxyItemModel()
        self.proxy_model.setSourceModel(self.data_model)

        self.treeView.setModel(self.proxy_model)
        self.treeView.header().hide()
        self.tableView.setModel(self.proxy_model)

        self.treeView.selectionModel().currentChanged.connect(self.set_root_index)
        self.treeView.selectionModel().currentChanged.connect(self.hide_show_table)

        self.show()

    def set_root_index(self, index):
        self.tableView.setRootIndex(index)
        self.proxy_model.set_root_index(index)

    def hide_show_table(self, index):
        if self.proxy_model.is_order(index):
            self.tableView.show()
        else:
            self.tableView.hide()

    def set_up_data_model(self):
        root = self.data_model.root
        group1 = GroupNode(root, name='G1', description='Order Group 1')
        group2 = GroupNode(root, name='G2', description='Order Group 2')
        order1 = OrderNode(group1, order_id='123', order_date='17.10.2018')
        item1 = ItemNode(order1, name='Item 1', price='345')
        item2 = ItemNode(order1, name='Item 2', description='item number 2', price='99')
        order2 = OrderNode(group1, order_id='987')


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ui = MainWindow()

    sys.exit(app.exec_())

现在,此代码导致根本不显示水平标题。但是,当我在0 <= section < len(header_list)方法中将headerData中的0 < section < len(header_list)替换为section = 0时,标题将正确显示-第一列只是空的。 section = 0时,代码中必须有某些操作,但是对于我一生来说,我不知道它是什么。我看了一下调试器,发现roleheader_list的{​​{1}}都具有预期值。

任何帮助将不胜感激。我试图删除不需要的所有内容,以获得一个最小的工作示例,但结果仍然很冗长,对此感到抱歉。

1 个答案:

答案 0 :(得分:0)

您必须验证该部分为Qt::DisplayRole,并且角色为def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole): if self.sourceModel() and self.root_index.isValid(): if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole: header_list = self.sourceModel().data(self.root_index, self.horizontal_header_role) if header_list and 0 <= section < len(header_list): return header_list[section] return super(ProxyItemModel, self).headerData(section, orientation, role) ,在您的情况下,您没有进行第二次验证,因此第二次验证被覆盖,从而产生了该错误。

import sys
from PyQt5.QtGui     import *
from PyQt5.QtCore    import *
from PyQt5.QtWidgets import *

class MyButton(QMainWindow):              
    def __init__(self,parent=None):
        super().__init__(parent)

        btn1 = QPushButton("Click or `Ctrl+Q`", clicked=self.close)
        btn1.setShortcut(QKeySequence("Ctrl+Q"))

        btn2    = QPushButton("QKeySequence.Close", 
                              clicked=lambda: print("\n Please Press -> Ctrl+W"))
        quitAct = QAction("Close", btn2, triggered=self.close)
        quitAct.setShortcuts(QKeySequence.Close)                     # <<<=======
        btn2.addAction(quitAct)

        btn3 = QPushButton("Click or `Ctrl+P`", clicked=lambda: print("Hello Kajsa"))
        btn3.setShortcut(QKeySequence("Ctrl+P"))

        centralWidget = QWidget()
        self.setCentralWidget(centralWidget)        
        v_layout = QVBoxLayout(centralWidget)
        v_layout.addWidget(btn1)
        v_layout.addWidget(btn2)
        v_layout.addWidget(btn3)

if __name__ == '__main__':
    app=QApplication([])
    mb = MyButton()
    mb.show()
    app.exec_()

enter image description here