diff --git a/bgdemo/widget.cpp b/bgdemo/widget.cpp index 4ba1eab..fcf8c34 100644 --- a/bgdemo/widget.cpp +++ b/bgdemo/widget.cpp @@ -2,7 +2,8 @@ #include "ui_widget.h" #include "qevent.h" #include "qdebug.h" - +#include +#include Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); @@ -23,6 +24,7 @@ bool Widget::eventFilter(QObject *watched, QEvent *evt) static int index = 1; static QPoint mousePoint; static bool mousePressed = false; + static bool is_smooth_scrolling = false; QMouseEvent *event = static_cast(evt); if (event->type() == QEvent::MouseButtonPress) { @@ -52,5 +54,29 @@ bool Widget::eventFilter(QObject *watched, QEvent *evt) } } + if (event->type() == QEvent::Wheel) { + QWheelEvent *event = static_cast(evt); + if(is_smooth_scrolling == false) { + if (event->delta() > 0) { + is_smooth_scrolling = true; + if (index == 5) { + index = 1; + } else { + index++; + } + } else if (event->delta() < 0) { + is_smooth_scrolling = true; + if (index != 1) { + index--; + } else { + index =5; + } + } + ui->widget->setStyleSheet(QString("background-image:url(:/image/%1.png);").arg(index)); + QTimer::singleShot(400, [&]() { is_smooth_scrolling = false; }); + return true; + } + } + return QWidget::eventFilter(watched, event); }