Send QWheelEvent to Child Scrollbar

  • I am trying to pass an event from a parent to a child, but when the scrollbar doesn't handle the event (ie when you try to scroll to the max when already at the max, value does not change and event is not handled) and return it instead sends the event to it's parent. So an infinite loop is created of the parent and child trying to get each other to answer a QWheelEvent.

    @class CustomWidget : QWidget
    CustomWidget() : sb(Qt::Orientation::Vertical, this) { }

    QScrollBar sb;
    void wheelEvent(QWheelEvent* ev) override
    { QApplication::sendEvent(&sb, ev); }


  • Nobody ? All I want is for the scrollbar to move the same amount when scrolling over the custom widget as if it were over the scrollbar. Using QScrollBar::setValue() won't guarantee it'll move the same amount unless there's a QStyle variable that specifies how far the scrollbar for QWheellEvent::delta ?

  • I haven't worked that much with events, but what I could understand is that you're not sending a wheel event just to the child scrollbar; you're actually creating another global wheel event - which causes the loop because for each wheel event another wheel event is created.

    The solution is either to send the wheel event directly to the widget you meant (I have no idea how to do that), or to reimplement the "QScrollbar::event()": event handler function to react to the event. If the main QScrollbar widget is capturing the event so the child QScrollbar is not answering the same event, I think you may manually call the "QScrollbar::event()": function with a QEvent::Wheel parameter (a wheel event).

    Sorry for not being of any more help, I'm still a beginner in Qt and I found these solutions just by reading the docs.

  • That worked perfectly (calling sb.event(ev) in wheelEvent), I should have read more into what QApplication::sendEvent did.

  • e: error posting...

  • I'm glad it helped ;)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.