Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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:

        TableView{
            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
                    }
                }
            }
        }//TableView
    

    Model class header file is as follows:

    class DatagridTableModel: public QAbstractTableModel
    {
    
        Q_OBJECT
        enum TableRoles { TableDataRole = Qt::UserRole + 1, HeadingRole };
    
    public:
        DatagridTableModel();
    
    
    
        // QAbstractItemModel interface
    public:
        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;
    
    private:
        QVector<QVector<QVariant>> table;
    };
    
    
    

    headers.JPG

    headers do not stay as I scroll

    noheaders.JPG


Log in to reply