Important: Please read the Qt Code of Conduct -

How to freeze TableView header row?

  • I am new to Qt. I have a TableView with header row that does not freeze when I scroll. Basically, I want column titles to stay on top in the visible area. I also have another issue related to scrollbars I will create another topic for that. Here is my TableView code:

            id: tableViewOutput
            anchors.fill: parent
            columnSpacing: 1
            rowSpacing: 1
            clip: true
            model: DatagridTableModel {}
            delegate: Rectangle {
                border.color: "black"
                implicitHeight: datacell.implicitHeight+10
                implicitWidth: datacell.implicitWidth+30
                border.width: 1
                Rectangle {
                    anchors.fill: parent
                    anchors.bottomMargin: 1
                    anchors.rightMargin: 1
                    color: heading ? '#757575':"#EEEEEE"
                    Text {
                        id: datacell
                        text: tabledata
                        wrapMode: Text.Normal
                        anchors.fill: parent
                        anchors.margins: 10
                        color: heading ? 'white':"black"
                        font.pixelSize: 10
                        verticalAlignment: Text.AlignVCenter

    Model class header file is as follows:

    class DatagridTableModel: public QAbstractTableModel
        enum TableRoles { TableDataRole = Qt::UserRole + 1, HeadingRole };
        // QAbstractItemModel interface
        int rowCount(const QModelIndex &parent) const;
        int columnCount(const QModelIndex &parent) const;
        QVariant data(const QModelIndex &index, int role) const;
        bool setData(const QModelIndex &index, const QVariant &value, int role);
        QVariant headerData(int section, Qt::Orientation orientation, int role) const;
        bool insertRows(int row, int count, const QModelIndex &parent);
        bool removeRows(int row, int count, const QModelIndex &parent);
        Qt::ItemFlags flags(const QModelIndex &index) const;
        QHash<int, QByteArray> roleNames() const;
        QVector<QVector<QVariant>> table;


    headers do not stay as I scroll


Log in to reply