almaghest
almaghest

Reputation: 113

spanning multiple columns, using QTreeView and QAbstractItemModel

I want to make a TreeView where the top level entries span all columns (e.g. they have one row), while children span multiple columns (e.g. they have multiple rows.)

I was trying to accomplish this with QTreeView.setFirstColumnSpanned. However, I can't really figure out where/when to call this (and maybe I'm going about things completely wrong to begin with.)

I thought maybe I could make a function in my view that would be called after the view is populated, to check for any items that need to have their spans updated.

However, I couldn't find any signals to connect to that might help me do this (I tried several, but none of them ever seemed to fire.)

I also tried reimplementing insertRows in my model and/or rowsInserted in my view, but they never seem to get called.

Here's an extremely simplified version of what I'm doing:

class MyModel(QtCore.QAbstractItemModel):
    def __init__(self, top_level_nodes):
        QtCore.QAbstractItemModel.__init__(self)
        self.top_level_nodes = top_level_nodes
        self.columns = 5

    def columnCount(self, parent):
        return self.columns

    def rowCount(self, parent):
        total = len(self.top_level_nodes)
        for node in self.top_level_nodes:
            total += len(node.subnodes)
        return total

    def data(self, index, role):

        if not index.isValid():
            return QtCore.QVariant()

        if role == QtCore.Qt.DisplayRole:
            obj = index.internalPointer()
            return obj.name

        return QtCore.QVariant()

    def index(self, row, column, parent):
        if not parent.isValid():
            if row > (len(self.top_level_nodes) - 1):
                return QtCore.QModelIndex()

            return self.createIndex(row, column, self.top_level_nodes[row])

        return QtCore.QModelIndex()

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

        node = index.internalPointer()
        if node.parent is None:
            return QtCore.QModelIndex()

        else:
            return self.createIndex(node.parent.row, 0, node.parent)

class FakeEntry(object):
    def __init__(self, name, row, children=[]):
        self.parent = None
        self.row = row
        self.name = 'foo'
        self.subnodes = children

class MainWindow(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)

        self.resize(800, 600)

        self.layout = QtGui.QVBoxLayout(self)
        self.tree_view = QtGui.QTreeView(self)
        self.layout.addWidget(self.tree_view)

        entry = FakeEntry('foo', 0, children=[])
        self.model = MyModel([entry])
        self.tree_view.setModel(self.model)

def main():
    app = QtGui.QApplication(sys.argv)
    frame = MainWindow()
    frame.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

This code runs fine. What I need to know is:

How can I make the entry created by FakeEntry span all 5 columns instead of creating 5 identical cells?

Other questions:

Upvotes: 4

Views: 3481

Answers (1)

Chris
Chris

Reputation: 1697

This works for me if I add a call to setFirstColumnSpanned after the setModel() call in MainWindow.__init__().

class MainWindow(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)

        self.resize(800, 600)

        self.layout = QtGui.QVBoxLayout(self)
        self.tree_view = QtGui.QTreeView(self)
        self.layout.addWidget(self.tree_view)

        entry = FakeEntry('foo', 0, children=[])
        self.model = MyModel([entry])
        self.tree_view.setModel(self.model)
        self.tree_view.setFirstColumnSpanned(0, QtCore.QModelIndex(), True)

I believe the view keeps track of the spanned columns internally, separate from the model. But it will reset the span information whenever setModel() is called. So, you should call setFirstColumnSpanned() after calling setModel() on the view. If your model is doing lazy loading with fetchMore(), you'll have to work out some way to inform the view that it should update the spans after the model has loaded new data.

With regard to what actually handles creating the rows/columns in the view, I think about that as a partnership between the model and the view. Once the view has a model set, it begins asking various questions to the model. For the top-most parent item (an invalid index), how many columns are there? Cool, there's 5. How many rows are there? Ok, 1. Just from that information the view's header can start to configure itself. At a certain point, the view iterates over the rows, columns, and children in the model, and asks each index for it's data in turn, for each data role. It can then start to create the delegates to display that data in the appropriate spot in the view.

Upvotes: 5

Related Questions