From b2d03972b845c84ec2d3181e15afce0819bc0652 Mon Sep 17 00:00:00 2001 From: wingsummer <1326224942@qq.com> Date: Sat, 28 Sep 2024 18:55:34 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20Toast=20=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E6=98=BE=E7=A4=BA=E5=A7=8B=E7=BB=88=E7=BD=AE=E9=A1=B6?= =?UTF-8?q?=E4=B8=94=E6=98=BE=E7=A4=BA=E6=97=B6=E7=A7=BB=E5=8A=A8=E7=AA=97?= =?UTF-8?q?=E4=BD=93=E5=B4=A9=E6=BA=83=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/control/toast.cpp | 22 +++++++++++++--------- src/control/toast.h | 6 +++--- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/control/toast.cpp b/src/control/toast.cpp index 15a80ff..99d6fda 100644 --- a/src/control/toast.cpp +++ b/src/control/toast.cpp @@ -19,9 +19,10 @@ static const int PADDING = 10; Toast::Toast(const QString &strContent, const QPixmap &icon, int nToastInterval, QWidget *parent) - : QWidget(parent), m_strContent(strContent), - m_nToastInterval(nToastInterval), m_nCurrentWindowOpacity(0), - m_nCurrentStayTime(0), m_nStatus(0), m_icon(icon), _parent(parent) { + : QDialog(parent), m_strContent(strContent), + m_nToastInterval(nToastInterval * TIMER_INTERVAL / 1000), + m_nCurrentWindowOpacity(0), m_nCurrentStayTime(0), m_nStatus(0), + m_icon(icon), _parent(parent) { Q_ASSERT(parent); init(); } @@ -32,6 +33,8 @@ void Toast::toast(QWidget *parent, const QPixmap &icon, static Toast *toast = nullptr; if (toast) { + toast->hide(); + toast->destroy(); toast->deleteLater(); } @@ -40,11 +43,13 @@ void Toast::toast(QWidget *parent, const QPixmap &icon, connect(toast, &Toast::destroyed, [&] { toast = nullptr; }); - auto e0 = new EventFilter(QEvent::Move, parent); - auto e1 = new EventFilter(QEvent::Resize, parent); + auto e0 = new EventFilter(QEvent::Move, toast); + auto e1 = new EventFilter(QEvent::Resize, toast); - auto callback = [&](QObject *, QEvent *) { - toast->setToastPos(toast->lastToastPos()); + auto callback = [](QObject *, QEvent *) { + if (toast) { + toast->setToastPos(toast->lastToastPos()); + } }; connect(e0, &EventFilter::eventTriggered, toast, callback); connect(e1, &EventFilter::eventTriggered, toast, callback); @@ -82,8 +87,7 @@ QSize Toast::calculateTextSize() { void Toast::init() { setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | - Qt::WindowStaysOnTopHint | Qt::WindowSystemMenuHint | - Qt::BypassWindowManagerHint); + Qt::WindowSystemMenuHint | Qt::BypassWindowManagerHint); setAttribute(Qt::WA_TranslucentBackground); setAttribute(Qt::WA_ShowWithoutActivating); diff --git a/src/control/toast.h b/src/control/toast.h index 09ca2c6..d10926a 100644 --- a/src/control/toast.h +++ b/src/control/toast.h @@ -1,13 +1,13 @@ #ifndef TOAST_H #define TOAST_H +#include #include #include #include #include -#include -class Toast : public QWidget { +class Toast : public QDialog { Q_OBJECT Q_PROPERTY(int fontPointSize READ fontPointSize WRITE setFontPointSize) @@ -22,7 +22,7 @@ public: static void toast(QWidget *parent, const QPixmap &icon, const QString &strContent, int fontPointSize = 20, - int nToastInterval = 3000); + int nToastInterval = 1500); virtual ~Toast();