Q:

QAbstractItemModel header on child tables

I created my own QAbstractItemModel with a tree-like structure and several sub tables like this:
group1
  - order1
     - item1
     - item2
  - order2
     - item3
group2
  - order3
     - item4
     - item5
     - item6

I my goal is it to have a tree and a table view. When I click on an order in the tree view I want to display a table with all items (and their attributes) in a table. So far I got it working. 
Now I obviously need headers for the table view, this is where I am somewhat stuck. I tried implementing the solution from this question but it is not working properly. This is what I got so far:
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_())

Now this code results in the horizontal header not being displayed at all. However, when I substitute 0 <= section < len(header_list) in the headerData method with 0 < section < len(header_list), the header is displayed correctly - except for the first column which is just empty. Something must be going on with the code when section = 0 but for the life of me I can't figure out what it is. I had a look with the debugger and saw that for section = 0 both role and header_list have the expected values.
Any help will be greatly appreciated. I tried to delete everything that is not needed to get a minimal working example, it still turned out quite lengthy though, sorry about that.

A:

You have to verify the section is Qt::Horizontal and the role is Qt::DisplayRole, in your case you did not do the second verification so it was overwritten generating that error.
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)

