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



  • 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


  • Qt Champions 2016

    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]


  • 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


  • Qt Champions 2016

    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



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


  • Qt Champions 2016

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



  • @mrjj

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


  • Qt Champions 2016

    @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 ?



  • 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.


  • Qt Champions 2016

    @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());
    }
    


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


  • Qt Champions 2016

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



  • 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.


  • Qt Champions 2016

    @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



  • 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 (!!) ??


  • Qt Champions 2016

    @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());
    }
    


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


  • Qt Champions 2016

    @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 ?



  • 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!


  • Qt Champions 2016

    @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/



  • 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!!


  • Qt Champions 2016

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.