Search code examples
pythonqtpyqtpyqt5qabstractlistmodel

PyQt5 and QAbstractListModel::columnCount


QTreeView ignores columnCount() of class inherited from QAbstractListModel and shows only first column:

import sys
from PyQt5.QtCore import QAbstractListModel
from PyQt5.QtWidgets import QApplication, QTreeView

class Model(QAbstractListModel):
    def columnCount(self, parent):
        return 3

    def data(self, index, role):
        return None

    def rowCount(self, parent):
        return 0

app = QApplication(sys.argv)

model = Model()
list_view = QTreeView()
list_view.setModel(model)
list_view.show()

app.exec_()

Relevant C++ code works fine.

What am I doing wrong?


Solution

  • From the Qt docs:

    The QAbstractListModel class provides an abstract model that can be subclassed to create one-dimensional list models. [emphasis added]

    But you clearly want a two-dimensional model, so use QAbstractItemModel instead.