Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Disable wheel event on QDateEdit



  • Hi,
    I want to disable the wheel event on all QDateEdit of my application. What is the best between subclassing Qdate edit and installing a global filter on the entire app?

    My code for the global event filter is the following and it works but I am not sure it is the most efficient way of doing it.

    bool MainWindow::eventFilter(QObject* target, QEvent *e)
    {
        if (qobject_cast<QDateEdit*>(target)!=NULL && e->type() == QEvent::Wheel){
            return true;
        }
        return false;
    }
    




  • Hi! Thanks for your answer.
    What do you mean? Subclassing QDateEdit, then getting the spinbox and filter the wheel event on the spinbox?



  • @Thombou
    Yes, that is the neater way, but requires you to subclass all your existing QDateEdits. If you start out subclassing all widgets you use it's easy. If you don't want to change your code then your original code seems fine.



  • Thanks for the advice!
    Small follow up question.

    @JonB said in Disable wheel event on QDateEdit:

    If you start out subclassing all widgets you use it's easy

    You mean that it's agood practice to subclass all kind of widgets I use? I did not know that. Is it how you do it?



  • @Thombou
    If starting out on a fair sized project using lots of widgets, I would give myself a ZzLineEdit, ZzCombBox, ZzDateEdit etc. (or you could put them in a namespace) directly derived from QLineEdit, QCombBox, QDateEdit, and use those for all the widgets I create. It's only a couple of lines of code done once. You never know when you might discover you need to add something in the subclass it for some reason....

    But in your case if you don't want to go hunt down all your existing QDateEdits, your eventFilter() solution will do, it's only an extra if.



  • @JonB Thank you! It always good to know some good practice. It is something I miss a bit... If you have any recommendation where I could find more tips like this, let me know!


Log in to reply