From 44177aa13c1c7f7207084aa04762c36d5a232da4 Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Mon, 7 Dec 2020 01:05:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E5=90=88toast?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Qss.cpp | 82 +++++++++++++++++++++++++++++++++++++++++++++++ Qss.h | 25 +++++++++++++++ forms/qsstoast.ui | 32 ++++++++++++++++++ qsswraper.pri | 1 + 4 files changed, 140 insertions(+) create mode 100644 forms/qsstoast.ui diff --git a/Qss.cpp b/Qss.cpp index 685b7c7..bb3dae4 100644 --- a/Qss.cpp +++ b/Qss.cpp @@ -16,6 +16,12 @@ #include #include "windows.h" #include "winuser.h" +#include +#include +#include +#include +#include + #define QSSDIALOG_SHADOW_WIDTH 12 //QFrame#dialog,QFrame#messagebox padding #define QSSDIALOG_BODER_WIDTH 0 @@ -268,6 +274,7 @@ QssMainWindow::QssMainWindow(QWidget *parent/* = 0*/, Qt::WindowFlags flags/* = m_rcNormal = this->parentWidget()->geometry(); m_rcNormalCentral = this->geometry(); connect(this->titleBar(),SIGNAL( OnMaxOrRestore(bool )),this,SLOT(OnMaxOrRestore(bool))); + } QssMainWindow::~QssMainWindow() @@ -1544,3 +1551,78 @@ QssPushButton::QssPushButton(QWidget *parent, QString objName): this->setObjectName(objName); } + + +QssToastWidget::QssToastWidget(QWidget *parent) + : QWidget(parent) +{ + ui.setupUi(this); + + setWindowFlags(windowFlags() | Qt::FramelessWindowHint | Qt::Tool);// 无边框 无任务栏 + setAttribute(Qt::WA_TranslucentBackground, true); // 背景透明 +} + +QssToastWidget::~QssToastWidget() +{ + +} + + +void QssToastWidget::setText(const QString& text) +{ + ui.label->setText(text); +} + +void QssToastWidget::showAnimation(int timeout /*= 2000*/) +{ + // 开始动画 + QPropertyAnimation *animation = new QPropertyAnimation(this, "windowOpacity"); + animation->setDuration(1000); + animation->setStartValue(0); + animation->setEndValue(1); + animation->start(); + show(); + + QTimer::singleShot(timeout, [&] + { + // 结束动画 + QPropertyAnimation *animation = new QPropertyAnimation(this, "windowOpacity"); + animation->setDuration(1000); + animation->setStartValue(1); + animation->setEndValue(0); + animation->start(); + connect(animation, &QPropertyAnimation::finished, [&] + { + close(); + deleteLater();// 关闭后析构 + }); + }); +} + +void QssToastWidget::showTip(const QString& text, QWidget* parent /*= nullptr*/) +{ + QssToastWidget* toast = new QssToastWidget(parent); + toast->setWindowFlags(toast->windowFlags() | Qt::WindowStaysOnTopHint); // 置顶 + toast->setText(text); + toast->setStyleSheet("font:bold;font-size:24px;color:rgb(255,255,255);"); + toast->adjustSize(); //设置完文本后调整下大小 + + // 测试显示位于主屏的70%高度位置 + QScreen* pScreen = QGuiApplication::primaryScreen(); + toast->move((pScreen->size().width() - toast->width()) / 2, + pScreen->size().height() * 5 / 10); + toast->showAnimation(1000); +} + +void QssToastWidget::paintEvent(QPaintEvent *event) +{ + QPainter paint(this); + paint.begin(this); + auto kBackgroundColor = QColor(255, 255, 255); + kBackgroundColor.setAlpha(0.0 * 255);// 透明度为0 + paint.setRenderHint(QPainter::Antialiasing, true); + paint.setPen(Qt::NoPen); + paint.setBrush(QBrush(kBackgroundColor, Qt::SolidPattern));//设置画刷形式 + paint.drawRect(0, 0, width(), height()); + paint.end(); +} diff --git a/Qss.h b/Qss.h index beeddc0..0032701 100644 --- a/Qss.h +++ b/Qss.h @@ -13,6 +13,8 @@ #include #include "windows.h" #include "winuser.h" +#include "ui_qsstoast.h" + class QPushButton; class QLabel; @@ -408,6 +410,29 @@ private: QRect m_rcValid; }; + +class QssToastWidget : public QWidget +{ + Q_OBJECT + +public: + QssToastWidget(QWidget *parent = Q_NULLPTR); + ~QssToastWidget(); + + void setText(const QString& text); + + void showAnimation(int timeout = 2000);// 动画方式show出,默认2秒后消失 + +public: + // 静态调用 + static void showTip(const QString& text, QWidget* parent = nullptr); + +protected: + virtual void paintEvent(QPaintEvent *event); + +private: + Ui::Toast ui; +}; #define tipBox(text) QssMessageBox::tips(text) #define warnBox(text) QssMessageBox::warn(text) #define errBox(text) QssMessageBox::error(text) diff --git a/forms/qsstoast.ui b/forms/qsstoast.ui new file mode 100644 index 0000000..9d0aa15 --- /dev/null +++ b/forms/qsstoast.ui @@ -0,0 +1,32 @@ + + + Toast + + + + 0 + 0 + 932 + 59 + + + + Form + + + + + 170 + 10 + 231 + 31 + + + + TextLabel + + + + + + diff --git a/qsswraper.pri b/qsswraper.pri index 49ec569..a8b4977 100644 --- a/qsswraper.pri +++ b/qsswraper.pri @@ -6,4 +6,5 @@ HEADERS += $$PWD/Qss.h SOURCES += $$PWD/Qss.cpp RESOURCES += $$PWD/qss.qrc +FORMS += $$PWD/forms/qsstoast.ui