What's the easiest way to get notified when the user scrolls down the QScrollArea?

Just as QPushButton

provides a clicked()

default signal , I expected QScrollArea

to have sliderChanged()

or a similar signal. I wonder what QScrollBar

has such a signal.

All I would like to do is find out how much of the huge widget inside the scroll area is visible when the user scrolls through it.

There are many solutions, none of which strike me as elegant:

  • subclass QScrollArea

  • subclassing the widget inside the scroll area and re-executing its paint event.
  • create custom view using QScrollBar

  • periodically poll the position of the widget within the scroll area. This seems like the worst solution.

Is there a way without subclassing?

+3


source to share


1 answer


A signal QAbstractSlider::valueChanged()

that is emitted when the value of the slider changes, with the new value of the slider as an argument. This will notify you as soon as you scroll through your view.

WRT second problem, none of the above points are required. You need:

1) Get the position of the inner widget (if any) associated with the scroll area:

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

      



Used for negative coordinates if you are scrolling the view down / right or zero without scrolling.

2) Get the size of the visible area

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

      

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

+4


source







All Articles