I have a QTableView
that I need to "lock" a selected row. The requirements are that the selected row must be moved to the middle of the table and then the scrolling of the table must be disabled.
This is easy enough with a big model and selecting a row somewhere in the middle of the model:
ui->tableView->scrollTo(ui->tableView->currentIndex(), QAbstractItemView::PositionAtCenter );
However, if the selected row is near the top or the bottom of the model then it will not be displayed in the middle of the table. Due to not being able to scroll past the last item.
I have looked at the QScrollbar
of the table:
QScrollBar *scrollbar = ui->tableView->verticalScrollBar();
scrollbar->setMaximum( scrollbar->maximum() + scrollbar->singleStep()*10 );
This adds ten white spaces to the bottom of the table and if I now "lock" a row near the bottom, it will be displayed in the middle of the TableView. This works great.
However, trying to add the white spaces to the top of the table, doesn't seem to work correctly:
scrollbar->setMinimum( scrollbar->minimum() - scrollbar->singleStep()*10 );
The code above does add the white space, but it breaks the tableview with some weird behavior. The data rows are invisible and only by clicking on them (selecting them) do they appear visible again, only to disappear when the table loses focus.
Is there a limitation to the QScrollbar::Minimum
value? Can I not make it negative? Or is there something I'm missing here?