Important: Please read the Qt Code of Conduct -

Horizontalheaderview with QSortFilterProxyModel

  • Hi,

    I have a question: I have a QAbstractTableModel which sets the headerData. Then I filter it with QSortFilterProxyModel and in QML I use the QuickControls2 TableView for displaying it, together with a Horizontalheaderview.

    The problem: The headers don't get displayed if I put a QSortFilterProxymodel inbetween. If I set the TableView directly to QAbstractTableModel the headers get displayed correct.

    From the HorizontalHeaderView documentation:

    model is a QAbstractTableModel, its horizontal headerData() will be accessed.
    If model is a QAbstractItemModel other than QAbstractTableModel, model's data() will be accessed.

    Is it possible that QSortFilterProxyModel makes a QAbstractItemModel out of my tablemodel and therefore it stops working? Or does someone got this working?

  • Ok, the problem seems to linked to the following from the HorizontalHeaderView documentation:

    The header displays data from the {syncView}'s model by default, but can also have its own model. If the model is a QAbstractTableModel, then the header will display the model's horizontal headerData(); otherwise, the model's data().

    So the headerData ist not taken, which seems to be a weird default behaviour.

  • In case someone wants to reproduce this:
    headers are displayed correctly if if use tableModel in the qml file, and it stops working if I use sortModel


    import QtQuick 2.15
    import QtQuick.Window 2.15
    import Qt.labs.qmlmodels 1.0
    import QtQuick.Controls 2.15
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
        HorizontalHeaderView {
            id: horizontalHeader
            syncView: tableView
            anchors.left: tableView.left
        VerticalHeaderView {
            id: verticalHeaderView
            syncView: tableView
            anchors.right: tableView.left
        TableView {
            id: tableView
            anchors.fill: parent
            anchors.margins: 50
            model: sortModel
            delegate: Rectangle {
                width: 30
                height: 30
                border.color: "black"
                color: "black"
                text: model.display


    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    #include <QSortFilterProxyModel>
    #include "tablemodel.h"
    int main(int argc, char *argv[])
      QGuiApplication app(argc, argv);
      QQmlApplicationEngine engine;
      const QUrl url(QStringLiteral("qrc:/main.qml"));
      QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                       &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
      }, Qt::QueuedConnection);
      TableModel tm;
      QSortFilterProxyModel sortModel;
      return app.exec();


    #ifndef TABLEMODEL_H
    #define TABLEMODEL_H
    #include <QAbstractTableModel>
    class TableModel : public QAbstractTableModel
      explicit TableModel(QObject *parent = nullptr);
      // Header:
      QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
      // Basic functionality:
      int rowCount(const QModelIndex &parent = QModelIndex()) const override;
      int columnCount(const QModelIndex &parent = QModelIndex()) const override;
      QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    #endif // TABLEMODEL_H


    #include "tablemodel.h"
    TableModel::TableModel(QObject *parent)
      : QAbstractTableModel(parent)
    QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
      return section;
    int TableModel::rowCount(const QModelIndex &parent) const
      if (parent.isValid())
        return 0;
     return 5;
    int TableModel::columnCount(const QModelIndex &parent) const
      if (parent.isValid())
        return 0;
      return 7;
    QVariant TableModel::data(const QModelIndex &index, int role) const
      if (!index.isValid())
        return QVariant();
      return QVariant::fromValue(QString::number(index.row())+","+QString::number(index.column()));

Log in to reply