Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Horizontalheaderview with QSortFilterProxyModel
Forum Updated to NodeBB v4.3 + New Features

Horizontalheaderview with QSortFilterProxyModel

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 1 Posters 474 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maxwell31
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      maxwell31
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        maxwell31
        wrote on last edited by
        #3

        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

        main.qml

        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
                anchors.top: tableView.top
            }
            TableView {
        
                id: tableView
                anchors.fill: parent
                anchors.margins: 50
        
                model: sortModel
                delegate: Rectangle {
                    width: 30
                    height: 30
                    border.color: "black"
                    Text{
                    color: "black"
                    text: model.display
        
                    }
                }
            }
        }
        
        

        main.cpp

        #include <QGuiApplication>
        #include <QQmlApplicationEngine>
        #include <QQmlContext>
        #include <QSortFilterProxyModel>
        
        #include "tablemodel.h"
        int main(int argc, char *argv[])
        {
          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        
          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)
              QCoreApplication::exit(-1);
          }, Qt::QueuedConnection);
        
          TableModel tm;
          engine.rootContext()->setContextProperty("tableModel",&tm);
          QSortFilterProxyModel sortModel;
          sortModel.setSourceModel(&tm);
          engine.rootContext()->setContextProperty("sortModel",&sortModel);
          engine.load(url);
        
          return app.exec();
        }
        
        

        tablemodel.h

        #ifndef TABLEMODEL_H
        #define TABLEMODEL_H
        
        #include <QAbstractTableModel>
        
        class TableModel : public QAbstractTableModel
        {
          Q_OBJECT
        
        public:
          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;
        
        private:
        };
        
        #endif // TABLEMODEL_H
        

        tablemodel.cpp

        #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()));
        }
        
        
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved