0

I would like to scroll my QScrollbar to center; I thought it was easy, but

QScrollBar *bar = ui->scrollArea->horizontalScrollBar();
bar->setValue(bar->maximum()/2);
bar->update();
ui->scrollArea->update();

doesn't do the job. What goes wrong?

László Papp
  • 51,870
  • 39
  • 111
  • 135
Bug Fisher
  • 13
  • 3

2 Answers2

2

In Qt's documentation the actual document length is defined by

document length = maximum() - minimum() + pageStep() (See QScrollBar Class Reference)

So try replacing

int center = (min+max)/2;

with

int center = (max+min+bar->pagestep())/2;
awpitt13
  • 155
  • 9
0

A QScrollBar has a minimum, too. So to center a scrollbar:

int max = bar->maximum();
int min = bar->minimum();
int center = (  min + max ) / 2;
bar->setValue( center );
epsilon
  • 2,849
  • 16
  • 23
Dimitry Ernot
  • 6,256
  • 2
  • 25
  • 37