Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] How to place frozen column on the right side of table
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] How to place frozen column on the right side of table

Scheduled Pinned Locked Moved General and Desktop
qtableviewcolumn
22 Posts 2 Posters 11.0k Views 2 Watching
  • 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
    Mr. Kibu
    wrote on last edited by
    #21

    I got it!

    Here is the picture of the result: http://postimg.org/image/fyh3pwl7h/

    I have got tables with many columns and I wanted to freeze one column (e.g. the last one) on the right side of the table-widget. In my case I give a fix width for the frozen column.

    Here is the code of my freezetablewidget.cpp:

    #include "freezetablewidget.h"
    
    #include <QScrollBar>
    #include <QHeaderView>
    
    FreezeTableWidget::FreezeTableWidget(QAbstractItemModel * model)
    {
          setModel(model);
          //setColumnHidden(model->columnCount()-1,true); // hide the last column
          frozenTableView = new QTableView(this);
    
          init();
    
          //connect the headers and scrollbars of both tableviews together
    
    //      connect(horizontalHeader(),SIGNAL(sectionResized(int,int,int)), this,
    //              SLOT(updateSectionWidth(int,int,int)));
          connect(verticalHeader(),SIGNAL(sectionResized(int,int,int)), this,
                  SLOT(updateSectionHeight(int,int,int)));
    
          connect(frozenTableView->verticalScrollBar(), SIGNAL(valueChanged(int)),
                  verticalScrollBar(), SLOT(setValue(int)));
          connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
                  frozenTableView->verticalScrollBar(), SLOT(setValue(int)));
    
    
    }
    
    FreezeTableWidget::~FreezeTableWidget()
    {
          delete frozenTableView;
    }
    
    void FreezeTableWidget::init()
    {
          frozenTableView->setModel(model());
          frozenTableView->setFocusPolicy(Qt::NoFocus);
          frozenTableView->verticalHeader()->hide();
          frozenTableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    
          viewport()->stackUnder(frozenTableView);
    
    
          frozenTableView->setStyleSheet("QTableView { border: none;"
                                         "background-color: #8EDE21;"
                                         "selection-background-color: #999}"); //for demo purposes
          frozenTableView->setSelectionModel(selectionModel());
    
          for (int col = 0; col < model()->columnCount()-1; ++col) // ! hide all other but NOT last
                    frozenTableView->setColumnHidden(col, true);
    
          frozenTableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
          frozenTableView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
          frozenTableView->show();
    
          updateFrozenTableGeometry();
    
          setHorizontalScrollMode(ScrollPerPixel);
          setVerticalScrollMode(ScrollPerPixel);
          frozenTableView->setVerticalScrollMode(ScrollPerPixel);
    }
    
    
    void FreezeTableWidget::updateSectionWidth(int logicalIndex, int /* oldSize */, int newSize)
    {
    //      if (logicalIndex == 0){
    //            frozenTableView->setColumnWidth(0, newSize);
    //            updateFrozenTableGeometry();
    //      }
    }
    
    void FreezeTableWidget::updateSectionHeight(int logicalIndex, int /* oldSize */, int newSize)
    {
          frozenTableView->setRowHeight(logicalIndex, newSize);
    }
    
    void FreezeTableWidget::resizeEvent(QResizeEvent * event)
    {
          QTableView::resizeEvent(event);
          updateFrozenTableGeometry();
     }
    
    QModelIndex FreezeTableWidget::moveCursor(CursorAction cursorAction,
                                              Qt::KeyboardModifiers modifiers)
    {
    //      QModelIndex current = QTableView::moveCursor(cursorAction, modifiers);
    
    //      if (cursorAction == MoveLeft && current.column() > 0
    //              && visualRect(current).topLeft().x() < frozenTableView->columnWidth(0) ){
    //            const int newValue = horizontalScrollBar()->value() + visualRect(current).topLeft().x()
    //                                 - frozenTableView->columnWidth(0);
    //            horizontalScrollBar()->setValue(newValue);
    //      }
    //      return current;
    }
    
    void FreezeTableWidget::scrollTo (const QModelIndex & index, ScrollHint hint){
    //    if (index.column() > 0)
    //        QTableView::scrollTo(index, hint);
    }
    
    
    void FreezeTableWidget::updateFrozenTableGeometry()
    {
    
        frozenTableView->setGeometry(horizontalHeader()->width()-73 ,frameWidth(), 100,viewport()->height()+horizontalHeader()->height());
    
    }
    

    Thank you to mrjj for your help!!

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #22

      Super!
      Good work
      Remember to mark it as SOLVED ifpossible.
      Happy coding :)

      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