QComboBox 下拉框禁用鼠标滚动改变当前项

//鼠标滚动的事件过滤
class ObjectIgnorQComboBoxWheel : public QObject {
    Q_OBJECT
public:
    explicit ObjectIgnorQComboBoxWheel(QObject *parent = nullptr) : QObject(parent) {}

    bool eventFilter(QObject *obj, QEvent *event) override {
        if(obj->metaObject()->className() == QStringLiteral("QComboBox")){
            if (event->type() == QEvent::Wheel)//鼠标滚轮事件
            {
                return true;//禁用下拉框的滚轮改变项的功能
            }
        }
        return QObject::eventFilter(obj, event);
    }
};

使用

QComboBox* widget=new QComboBox();
widget->installEventFilter(new ObjectIgnorQComboBoxWheel());
QStringList  list;list<<"test1";list<<"test2";
widget->addItems(list);

posted @ 2025-06-23 18:42  小城熊儿  阅读(99)  评论(0)    收藏  举报