
 最近在写一个模拟器,在item中添加了很多的控件,这些控件默认是支持鼠标滚动事件的。在数据量特别大的时候,及容易不小心就把数据给修改了而不自知。所有,我们这里需要禁用掉这些控件的鼠标滚轮事件。
 实现的思想很简单,集成Qt提供的控件,并重写wheelEvent.为了偷懒,笔者进行了如下的设计:
#ifndef NOWHEELUICONTROLS_H
#define NOWHEELUICONTROLS_H
#include <QWheelEvent>
#include <QComboBox>
#include <QDoubleSpinBox>
#include <QSpinBox>
#include <QListWidget>
#define DEFINE_NOWHEEL_CONTROLS(parent)  \
class NoWheel##parent : public parent { \
public:\
    using parent::parent; /* Use the constructor of the parent class */ \
protected:\
    void wheelEvent(QWheelEvent* event) override { event->ignore(); } \
};
DEFINE_NOWHEEL_CONTROLS(QComboBox)
DEFINE_NOWHEEL_CONTROLS(QDoubleSpinBox)
DEFINE_NOWHEEL_CONTROLS(QSpinBox)
DEFINE_NOWHEEL_CONTROLS(QListWidget)
// add new here
#endif // NOWHEELUICONTROLS_H
                ![[Swift]组件化开发](https://img-blog.csdnimg.cn/direct/f37a3b1d57174d01ae104218fa1b11d3.png)

















