From 8198e81ec5e77e61291577fe35f0f28d66d23f58 Mon Sep 17 00:00:00 2001 From: taiyunqiang Date: Wed, 18 Nov 2020 10:13:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=BB=9A=E8=BD=AE=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bgdemo/widget.cpp | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) 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); }