PDA

View Full Version : QTableWidget Not Scrolling, Centering Properly



soupfly
31st March 2016, 05:12
Hi all,

I'm stumped trying to understand why I my QTableWidget to scroll like I think it should. I'm appending rows to a QTableWidget and I want to center the view on a particular row.



ui->tableWidget->scrollToTop();
ui->tableWidget->scrollToBottom();


Testing one at a time, both of these work great and the behavior is as expected. The scrollbars\view stay either at the top or the bottom. However when I try to center the view it doesn't work at all:



ui->tableWidget->scrollToItem(ui->tableWidget->itemAt(center, 0));


Center is an integer representing the row I want to center the view on. I tried switching "center,0" with "0,center" it didn't seem to help. I check the return value for itemAt and it's returning a non-NULL value.

My workaround (from: "QListWidget in a tab not scrolling on setCurrentRow() (http://www.qtcentre.org/threads/8162-QListWidget-in-a-tab-not-scrolling-on-setCurrentRow())") was to do this:



ui->tableWidget->setCurrentCell(center, 0);
QModelIndex index = ui->tableWidget->currentIndex();
ui->tableWidget->scrollTo(index, QAbstractItemView::PositionAtCenter);
ui->tableWidget->setCurrentCell(-1, -1);


Which works but seems horrible. The last setCurrentCell() was to un-highlight the cell...

For my own knowledge can you please help to explain what I was doing wrong? Thanks in advance.

anda_skoa
31st March 2016, 08:50
Did you check the documentation of the QTableWidget::itemAt() and then decided to ignored it or is reading the documentation something you delegate to others?

Cheers,
_

Pirus311
8th February 2017, 05:51
Hello!
I have the same problem in QT 5.5

Solution:
1) Find in the file Qt_source\qtbase\src\widgets\itemviews\qtableview. cpp function


void QTableView::scrollTo(const QModelIndex &index, ScrollHint hint)

2) Add string updateGeometries(); to this function



void QTableView::scrollTo(const QModelIndex &index, ScrollHint hint)
{

Q_D(QTableView);

updateGeometries();
.....
}


3) Go to Qt_source\qtbase\src\widgets and recompile Qt5Widgets.dll

It works well to me!


By analogy with the function in Qt_source\qtbase\src\widgets\itemviews\qabstractit emview.cpp


void QAbstractItemView::scrollToBottom()
{
Q_D(QAbstractItemView);
if (d->delayedPendingLayout) {
d->executePostedLayout();
updateGeometries();
}
verticalScrollBar()->setValue(verticalScrollBar()->maximum());
}