QTableWidget Not Scrolling, Centering Properly
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.
Code:
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:
Code:
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()") was to do this:
Code:
ui->tableWidget->setCurrentCell(center, 0);
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.
Re: QTableWidget Not Scrolling, Centering Properly
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,
_
Re: QTableWidget Not Scrolling, Centering Properly
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
2) Add string updateGeometries(); to this function
Code:
{
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
Code:
{
if (d->delayedPendingLayout) {
d->executePostedLayout();
updateGeometries();
}
verticalScrollBar()->setValue(verticalScrollBar()->maximum());
}