1

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?

DanC225
  • 161
  • 12
  • What if you instead of playing with scroll bar just add empty rows before the first one and after the last one? – vahancho Nov 07 '17 at 07:50
  • My answer _might_ be a solution to your problem: [SO: Stop QTableView from scrolling as data is added above current position](https://stackoverflow.com/a/42460216/7478597). – Scheff's Cat Nov 07 '17 at 07:50
  • @vahancho I thought about adding empty rows but this table and model is used all over the application and adding extra rows here makes it more complicated and a lot of extra work all over the application. – DanC225 Nov 07 '17 at 08:00
  • @Scheff Thanks, I don't think it'll help me with this specific problem, but I'm going to play around with your example and see what I can learn – DanC225 Nov 07 '17 at 08:00
  • OK. The centering of selected row seems to be your actual problem. Actually, you need to insert only one row at begin and end - adjusting its height with viewport height - height of selected row. I'm afraid that this is as inacceptable for you as insertion of multiple empty rows. Btw. is it really essential that even the first row must appear in vertical center of list? Isn't this unusual? – Scheff's Cat Nov 07 '17 at 08:04
  • How about adding the TableView to another QScrollArea and adding some space before and after the view? – SteakOverflow Nov 07 '17 at 08:12
  • yes, unfortunately its a requirement from the customer to have the "locked" row in the center. – DanC225 Nov 07 '17 at 08:43
  • if I add the tableview to a QScrollArea, won't the space created at the top then be above the Table header? I need the space between the header and the data – DanC225 Nov 07 '17 at 08:44

0 Answers0