[SOLVED] How to place frozen column on the right side of table
-
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/ -
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!!
-
Super!
Good work
Remember to mark it as SOLVED ifpossible.
Happy coding :)