7

I'm trying to make a custom widget, which is basically a bunch of textedits with a single vertical scroll bar on the right. To keep widget size determined, I have to find a width of the scrollbar, as texedits geometry depends on it. Simply calling scroll_bar->width(); for freshly created vertical bar returns 101, which is obviuosly wrong.

So, is there a way to determine vertical scroll bar width correctly in Qt 3.2.3?

akalenuk
  • 3,815
  • 4
  • 34
  • 56

3 Answers3

28
int w = qApp->style()->pixelMetric(QStyle::PM_ScrollBarExtent);
hank
  • 9,553
  • 3
  • 35
  • 50
1

I've also found

vsb->sliderRect().width(); 

which happens to be correct, but it looks bad in the code, as I have no interest in slider per se.

akalenuk
  • 3,815
  • 4
  • 34
  • 56
0

If you're using a scrollArea and getting the scrollbar's width a few pixels short when using Hank's answer, try the following:

int scrollBarWidth = qApp->style()->pixelMetric(QStyle::PM_ScrollBarExtent);
scrollBarWidth += ui->scrollArea->width() - ui->scrollAreaWidgetContents->width();

It seems that there's a small difference in size between the scrollArea and the scrollAreaWidgetContents.

MHDev
  • 1
  • 1