Search code examples
c++qtkeyboardmousewheelqslider

Disable QSlider's valueChanged on keyboard arrow keys


How can I disable QSlider's emitting SIGNAL valueChanged on keyboard arrows and mouseWheel scroll. By setting

    ui->horizontalSlider->setFocusPolicy(Qt::NoFocus);

the keyboards arrows keys are not emitting signals.

How can I do the same for mouse wheel scroll? I don't want to reimplement my custom Slider class from QSlider. Am asking for any work around or api, so that I can disable the keyboard and mouse wheel scroll responding to qslider.

The main intention is to avoid sending signals on every valueChange. So I have implemented the following and it's working fine

on_horizontalSlider_sliderReleased()
on_horizontalSlider_sliderPressed()

based on bool flag updated in these 2 slots,

on_horizontalSlider_valueChanged(int)

is doing the required operation...like after slider released, value is being added in the required application.

Request to provide approaches, examples for the work around.


Solution

  • class CBlockWheelEV: public QObject
    {
        Q_OBJECT
    public:
        CBlockWheelEV( QObject* parent ): QObject( parent ) {}
        bool eventFilter(QObject * pObject, QEvent * pEvent) override
        {
            if ( pEvent->type() == QEvent::Wheel )
            {
                qDebug() << pObject << pEvent;
                return true; // if return false - Scroll was unlocking
            }
            return false;
        }
    };
    
    ...
    ui->sldTest->installEventFilter( new CBlockWheelEV( ui->sldTest ) );
    ...
    

    You can use EventFilter to block emitting signals ValueChanged from QSlider by wheel rotating. But if you want some special behavior - you need control additional options in you EventFilter. EventFilter can manage many behavior of object without subclassing it.