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 Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved General and Desktop
qtableviewcolumn
22 Posts 2 Posters 9.1k 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
    Mr. Kibu
    wrote on 5 Sept 2015, 17:58 last edited by Mr. Kibu 9 Aug 2015, 10:45
    #1

    Hello!

    How do I place the last column from the table on the right side of the table in this example: http://doc.qt.io/qt-5/qtwidgets-itemviews-frozencolumn-example.html.

    Thank you!

    Franz

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 5 Sept 2015, 18:29 last edited by mrjj 9 May 2015, 18:31
      #2

      Hi
      its actually not really a column but another view that is sort put over the "frozen" one.
      To put to the right, you would have to change freezetablewidget.cpp to the following:
      Look for // ! for the changes. a few places. (you can just replace whole content of file)

      #include "freezetablewidget.h"
      
      #include <QScrollBar>
      #include <QHeaderView>
      
      //! [constructor]
      FreezeTableWidget::FreezeTableWidget(QAbstractItemModel * model)
      {
            setModel(model);
            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)));
      
      
      }
      //! [constructor]
      
      FreezeTableWidget::~FreezeTableWidget()
      {
            delete frozenTableView;
      }
      
      //! [init part1]
      void FreezeTableWidget::init()
      {
            frozenTableView->setModel(model());
            frozenTableView->setFocusPolicy(Qt::NoFocus);
            frozenTableView->verticalHeader()->hide();
            frozenTableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
      
            viewport()->stackUnder(frozenTableView);
      //! [init part1]
      
      //! [init part2]
            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->setColumnWidth(0, columnWidth(9) ); // !
      
            frozenTableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
            frozenTableView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
            frozenTableView->show();
      
            updateFrozenTableGeometry();
      
            setHorizontalScrollMode(ScrollPerPixel);
            setVerticalScrollMode(ScrollPerPixel);
            frozenTableView->setVerticalScrollMode(ScrollPerPixel);
      }
      //! [init part2]
      
      
      //! [sections]
      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);
      }
      //! [sections]
      
      
      //! [resize]
      void FreezeTableWidget::resizeEvent(QResizeEvent * event)
      {
            QTableView::resizeEvent(event);
            updateFrozenTableGeometry();
       }
      //! [resize]
      
      
      //! [navigate]
      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;
      }
      //! [navigate]
      
      void FreezeTableWidget::scrollTo (const QModelIndex & index, ScrollHint hint){
          if (index.column() > 0)
              QTableView::scrollTo(index, hint);
      }
      
      //! [geometry]
      void FreezeTableWidget::updateFrozenTableGeometry()
      {
      
          int sumwidth=0;// !
          for (int col = 0; col < model()->columnCount()-1; ++col) // !
                sumwidth+=columnWidth(col);
      
            frozenTableView->setGeometry(verticalHeader()->width() + frameWidth()+sumwidth,frameWidth(),
                                         columnWidth(0), viewport()->height()+horizontalHeader()->height());
      }
      //! [geometry]
      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mr. Kibu
        wrote on 7 Sept 2015, 08:25 last edited by
        #3

        Hi!

        Thank you for your fast answer!

        If I use your code above I have no frozen column. If I use the original function "updateFrozenTableGeometry" from the qt-example, the content of the last column is frozen on the left side.

        What is going wrong

        Franz

        M 1 Reply Last reply 7 Sept 2015, 08:33
        0
        • M Mr. Kibu
          7 Sept 2015, 08:25

          Hi!

          Thank you for your fast answer!

          If I use your code above I have no frozen column. If I use the original function "updateFrozenTableGeometry" from the qt-example, the content of the last column is frozen on the left side.

          What is going wrong

          Franz

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 7 Sept 2015, 08:33 last edited by
          #4

          hi
          That is strange.
          Just tested here at work, pasting the code to the example and got this
          http://postimg.org/image/snoeak43z/

          You did expand the window fully so last col was visible ?

          here is the full project, please check if that works for you
          https://www.dropbox.com/s/tjj557dbxcwhnbt/frozencolumnRight.zip?dl=0

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mr. Kibu
            wrote on 7 Sept 2015, 08:49 last edited by
            #5

            No, I see the hole tabel and the last column is visible.

            M 1 Reply Last reply 7 Sept 2015, 09:02
            0
            • M Mr. Kibu
              7 Sept 2015, 08:49

              No, I see the hole tabel and the last column is visible.

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 7 Sept 2015, 09:02 last edited by
              #6

              @Mr.-Kibu
              So even with the full project , last col is not frozen ?
              What Qt version are you using ?

              M 1 Reply Last reply 7 Sept 2015, 09:08
              0
              • M mrjj
                7 Sept 2015, 09:02

                @Mr.-Kibu
                So even with the full project , last col is not frozen ?
                What Qt version are you using ?

                M Offline
                M Offline
                Mr. Kibu
                wrote on 7 Sept 2015, 09:08 last edited by
                #7

                @mrjj

                I have the hole table including the last column that is not frozen.
                I use QT 5.4

                M 1 Reply Last reply 7 Sept 2015, 09:18
                0
                • M Mr. Kibu
                  7 Sept 2015, 09:08

                  @mrjj

                  I have the hole table including the last column that is not frozen.
                  I use QT 5.4

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 7 Sept 2015, 09:18 last edited by mrjj 9 Jul 2015, 09:22
                  #8

                  @Mr.-Kibu
                  and that is with the project "frozencolumnRight" ?

                  you mean, its green but not frozen or its not green at all ?

                  update:
                  just tried the "Frozen Column Example" unmodified and its possible to edit the green "column"
                  so its not really frozen on Qt 5.5, it seems ?

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Mr. Kibu
                    wrote on 7 Sept 2015, 09:37 last edited by
                    #9

                    The column in my project is not green and not frozen.

                    The last column in your project (frozencolumnRight) is green and fixed on a certain position but not on the right side of the table. Additional I can see the last column when I scroll horizontal.

                    Here you can see it: http://postimg.org/image/4fvoga64v/

                    When I change the width of the other columns, than the green column stays at the same place.

                    M 1 Reply Last reply 7 Sept 2015, 10:02
                    0
                    • M Mr. Kibu
                      7 Sept 2015, 09:37

                      The column in my project is not green and not frozen.

                      The last column in your project (frozencolumnRight) is green and fixed on a certain position but not on the right side of the table. Additional I can see the last column when I scroll horizontal.

                      Here you can see it: http://postimg.org/image/4fvoga64v/

                      When I change the width of the other columns, than the green column stays at the same place.

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 7 Sept 2015, 10:02 last edited by
                      #10

                      @Mr.-Kibu

                      It seems that its off for some reason.
                      Remember its actually another view that is put on top of the real col.
                      So if you change the col size of view 1 one and not for view 2, it will mismatch since than
                      cols are not same size anymore. For it to work, if you change frozen cols size you must do it on both views.

                      After you change col width , do you call updateFrozenTableGeometry() ?

                      it tried to calculate the overlay position (sumwidth)

                      void FreezeTableWidget::updateFrozenTableGeometry()
                      {
                      
                          int sumwidth=0;// !
                          for (int col = 0; col < model()->columnCount()-1; ++col) // !
                                sumwidth+=columnWidth(col);
                      
                            frozenTableView->setGeometry(verticalHeader()->width() +
                           frameWidth()+sumwidth,frameWidth(),columnWidth(0), viewport()->height()+horizontalHeader()->height());
                      }
                      
                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        Mr. Kibu
                        wrote on 7 Sept 2015, 10:27 last edited by
                        #11

                        I change only the col width in the window of your Frozen Column Example.

                        M 1 Reply Last reply 7 Sept 2015, 10:35
                        0
                        • M Mr. Kibu
                          7 Sept 2015, 10:27

                          I change only the col width in the window of your Frozen Column Example.

                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 7 Sept 2015, 10:35 last edited by
                          #12

                          ok and did you change for both views ?
                          like
                          frozenTableView->setColumnWidth(9, 100 );
                          setColumnWidth(9, 100 );

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            Mr. Kibu
                            wrote on 7 Sept 2015, 10:50 last edited by
                            #13

                            No, I only change the width of the col by dragging and dropping with the mouse. But when I change the column width in that way an than I change the size of the window, the green column with the content of the last column changes to the place were the last column of the underlaying table is. But not to the very right end of the table widget.

                            M 1 Reply Last reply 7 Sept 2015, 10:56
                            0
                            • M Mr. Kibu
                              7 Sept 2015, 10:50

                              No, I only change the width of the col by dragging and dropping with the mouse. But when I change the column width in that way an than I change the size of the window, the green column with the content of the last column changes to the place were the last column of the underlaying table is. But not to the very right end of the table widget.

                              M Offline
                              M Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 7 Sept 2015, 10:56 last edited by
                              #14

                              @Mr.-Kibu

                              ahh
                              i think you need fix also to use last col for mouse dragging to work. It still uses
                              Col 0 and not 9 (the last)
                              if you plan to have more than 9 cols maybe model()->columnCount() is better to use than "9"

                              void FreezeTableWidget::updateSectionWidth(int logicalIndex, int /* oldSize */, int newSize)
                              {
                                    if (logicalIndex == 0){ <-- 9
                                          frozenTableView->setColumnWidth(0, newSize); <-- 9
                                          updateFrozenTableGeometry();
                                    }
                              }
                              

                              also QModelIndex FreezeTableWidget::moveCursor still uses index 0

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                Mr. Kibu
                                wrote on 7 Sept 2015, 11:11 last edited by
                                #15

                                Ok, I have now the content of the last column in a green frozen column at the left side of the table. How can I place the view of the frozen column to the very right side of the table-widget (!!) ??

                                M 1 Reply Last reply 7 Sept 2015, 11:16
                                0
                                • M Mr. Kibu
                                  7 Sept 2015, 11:11

                                  Ok, I have now the content of the last column in a green frozen column at the left side of the table. How can I place the view of the frozen column to the very right side of the table-widget (!!) ??

                                  M Offline
                                  M Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on 7 Sept 2015, 11:16 last edited by
                                  #16

                                  @Mr.-Kibu
                                  ok, getting there. :)

                                  well I assume
                                  you mean on the very right side on top of the very last column.
                                  updateFrozenTableGeometry() places view 2 ( the frozen one) so to place it there
                                  we must sum up col size of all the cols before the last one to know how
                                  much to move it.

                                  void FreezeTableWidget::updateFrozenTableGeometry()
                                  {
                                  
                                      int sumwidth=0;// !
                                      for (int col = 0; col < model()->columnCount()-1; ++col) // ! <---- sum up
                                            sumwidth+=columnWidth(col);
                                  
                                        frozenTableView->setGeometry(verticalHeader()->width() +
                                       frameWidth()+sumwidth,frameWidth(),columnWidth(0), viewport()->height()+horizontalHeader()->height());
                                  }
                                  
                                  1 Reply Last reply
                                  0
                                  • M Offline
                                    M Offline
                                    Mr. Kibu
                                    wrote on 7 Sept 2015, 11:28 last edited by
                                    #17

                                    In the following picture you can see were I want to place the frozen column: http://postimg.org/image/egzx7ky6b/

                                    M 1 Reply Last reply 7 Sept 2015, 11:33
                                    0
                                    • M Mr. Kibu
                                      7 Sept 2015, 11:28

                                      In the following picture you can see were I want to place the frozen column: http://postimg.org/image/egzx7ky6b/

                                      M Offline
                                      M Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on 7 Sept 2015, 11:33 last edited by
                                      #18

                                      @Mr.-Kibu

                                      Ok, I understand.
                                      Just on the Brazil col.
                                      If its not aligned 100% straight on top then maybe something is off with
                                      updateFrozenTableGeometry ( the new version)

                                      So is it placed before or after or what is wrong ?

                                      1 Reply Last reply
                                      0
                                      • M Offline
                                        M Offline
                                        Mr. Kibu
                                        wrote on 7 Sept 2015, 11:37 last edited by
                                        #19

                                        No, I want to place the Brazil col (green and frozen) to the orange area of my posted picture. The width of the frozen column on the right side now could be a fix value!

                                        M 1 Reply Last reply 7 Sept 2015, 11:48
                                        0
                                        • M Mr. Kibu
                                          7 Sept 2015, 11:37

                                          No, I want to place the Brazil col (green and frozen) to the orange area of my posted picture. The width of the frozen column on the right side now could be a fix value!

                                          M Offline
                                          M Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on 7 Sept 2015, 11:48 last edited by mrjj 9 Jul 2015, 11:54
                                          #20

                                          @Mr.-Kibu
                                          ahh sorry, didn't maximize window so didn't see the orange area .

                                          In that case you can just use
                                          frozenTableView->setGeometry( width() + 200, ..)
                                          to move as far to the right as you want.
                                          You should also disable pdateFrozenTableGeometry() then
                                          as it would move it back on to of the other brazil.

                                          But why would you want that ?

                                          update:
                                          well you can just alter sumwidth start value to move it to the right

                                          like
                                          void FreezeTableWidget::updateFrozenTableGeometry()
                                          {
                                          int sumwidth=200;// !
                                          ...
                                          http://postimg.org/image/yzp7qsbw3/

                                          1 Reply Last reply
                                          0

                                          1/22

                                          5 Sept 2015, 17:58

                                          • Login

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