-1

By default QSlider reacts on mouse wheel only when cursor is on it. I want to control slider by mouse wheel not matter where the cursor is (of course only when my application has focus).

What's the best way to achieve that?

I just learned that mouse doesn't generate signals. So looks like I need to override wheelEvent in main window and use raise or lower method in it. But I guess that can cause double increment (or decrement) of slider value when mouse is hovering it.

So once again: What's the best way to achieve what I described?

leggo
  • 309
  • 2
  • 3
  • 11
  • 1
    I'd say your approach is correct, override `wheelEvent` in the main window and call the slider. Double increment can be prohibited by checking if the mouse is over the slider in the main window event for instance (multiple possibilities here). But keep in mind that you will run into problems if you have any other widget with scroll functionality. – Bowdzone Sep 04 '15 at 08:56
  • Slider is the only widget in application, that's why I want it to work that way. Maybe I can somehow disable default reaction on mouse wheel in slider? – leggo Sep 04 '15 at 09:50
  • 1
    Yes you can, as I stated you can check in your mainwindow whether the pointer is over the slider or you subclass the slider, override the wheel event as well and simply return there – Bowdzone Sep 04 '15 at 09:51
  • I was counting on some property inside QSlider or QAbstractSlider, that will allow me to disable this functionality. In that case I'll just check mouse position. Thanks. – leggo Sep 04 '15 at 10:00
  • You can also use [an event filter](http://doc.qt.io/qt-5/eventsandfilters.html#event-filters) which is a cleaner solution in my opinion. – Bowdzone Sep 04 '15 at 10:01
  • 1
    The double increment or decrement should not happen, because [`QSlider` will accept the event if it modifies the slider value](https://github.com/Vitallium/qt5/blob/master/qtbase/src/widgets/widgets/qabstractslider.cpp#L752), so the event won't be propagated any further. – thuga Sep 04 '15 at 12:38

1 Answers1

2

You can override the wheelEvent() in your MainWindow and redirect it to your slider control, that is indeed one option.

Another option is to install an event filter on the qApp instance. This even filter then can check whether the event type is QEvent::Wheel, and when the receiver is not the slider, then again redirect it to the slider. Should work just as well.

dhaumann
  • 1,590
  • 13
  • 25