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. How to freeze TableView header row?
Qt 6.11 is out! See what's new in the release blog

How to freeze TableView header row?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
1 Posts 1 Posters 685 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.
  • A Offline
    A Offline
    adqt
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    1

    • Login

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