[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
-
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
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 -
@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.
-
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()); }
-
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.
-
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
-
@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/
-
-
@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 rightlike
void FreezeTableWidget::updateFrozenTableGeometry()
{
int sumwidth=200;// !
...
http://postimg.org/image/yzp7qsbw3/