Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the simplest way to get notified whenever the user scrolls a QScrollArea?

Tags:

scrollbar

qt

Just as a QPushButton provides a default clicked() signal, I expected QScrollArea to have a sliderChanged() or similar signal. Interestingly, the QScrollBar does have such a signal.

All I would like to do is to know what part of the huge widget inside the scroll area is visible, whenever the user scrolls it.

There are many solutions, none of which seem elegant to me:

  1. subclass QScrollArea
  2. subclass the widget inside the scroll area, and re-implement its paint event.
  3. create a custom veiwport, using QScrollBar
  4. periodically poll the position of the widget inside the scroll area. This seems to be the worst solution.

Is there a way without subclassing?

like image 712
vsz Avatar asked Feb 10 '23 12:02

vsz


1 Answers

There is QAbstractSlider::valueChanged() signal that is emitted when the slider value has changed, with the new slider value as argument. This will notify you as soon as you scroll your view.

WRT the second problem, neither of mentioned points necessary. You need to:

1) Get the position of inner widget (if any) related to the scroll area:

QPoint p = scrollArea->widget()->pos();

It use to be a negative coordinates if you scrolled your view down/right or null without scrolling.

2) Get the size of the visible area

QSize s = scrollArea->viewport()->size();

With these two values you can construct a QRect that will represent the visible area of your inner widget.

like image 82
vahancho Avatar answered Apr 29 '23 19:04

vahancho