跳转到帖子

排行榜


热门内容

11/28/21 在所有区域 显示最高声誉的内容

  1. 1 点 积分
    先看效果图: Toast.h #ifndef TOAST_H #define TOAST_H #include <QDialog> namespace Ui { class Toast; } class Toast : public QDialog { Q_OBJECT public: explicit Toast(QWidget *parent = nullptr); ~Toast(); void setText(const QString& text); void setBgColor(const QString& color); void showAnimation(int timeout = 3000); public: static void tip(const QString& text); static void info(const QString& text); static void warn(const QString& text); static void err(const QString& text); static void succ(const QString& text); static void display(const QString& text,QString); protected: virtual void paintEvent(QPaintEvent *event); private: Ui::Toast *ui; }; #endif // TOAST_H Toast.cpp #include "Toast.h" #include "ui_Toast.h" #include <QPropertyAnimation> #include <QScreen> #include <QGuiApplication> #include <QPainter> #include <QTimer> Toast::Toast(QWidget *parent) : QDialog(parent), ui(new Ui::Toast) { ui->setupUi(this); setWindowFlags(windowFlags() | Qt::FramelessWindowHint | Qt::Tool); setAttribute(Qt::WA_TranslucentBackground, true); } Toast::~Toast() { delete ui; } void Toast::setText(const QString& text) { ui->label->setText(text); } void Toast::setBgColor(const QString &color) { QString css = "border-radius: 26px;color: #FFFFFF;font-family: microsoft yahei;font-size: 16px;padding-left:25px;padding-right:25px;"; if("ERROR" == color) { ui->label->setStyleSheet("background-color: #dc3545;"+css); }else if("SUCC" == color) { ui->label->setStyleSheet("background-color: #198754;"+css); } else if("WARNING" == color) { ui->label->setStyleSheet("background-color: #fd7e14;"+css); } else if("INFOMATION" == color) { ui->label->setStyleSheet("background-color: #009ef6;"+css); } else{ ui->label->setStyleSheet("background-color: rgba(0,0,0,0.80);"+css); } } void Toast::showAnimation(int timeout) { // 开始动画 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 Toast::tip(const QString &text) { display(text,"TIP"); } void Toast::info(const QString &text) { display(text,"INFOMATION"); } void Toast::warn(const QString &text) { display(text,"WARNING"); } void Toast::err(const QString &text) { display(text,"ERROR"); } void Toast::succ(const QString &text) { display(text,"SUCC"); } void Toast::display(const QString& text,QString color) { Toast* toast = new Toast(nullptr); toast->setWindowFlags(toast->windowFlags() | Qt::WindowStaysOnTopHint); // 置顶 toast->setText(text); toast->setBgColor(color); toast->adjustSize(); // 测试显示位于主屏的70%高度位置 QScreen* pScreen = QGuiApplication::primaryScreen(); toast->move((pScreen->size().width() - toast->width()) / 2, pScreen->size().height() * 7 / 10); toast->showAnimation(); } void Toast::paintEvent(QPaintEvent *event) { } Toast.ui <?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>Toast</class> <widget class="QWidget" name="Toast"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>170</width> <height>52</height> </rect> </property> <property name="minimumSize"> <size> <width>0</width> <height>52</height> </size> </property> <property name="maximumSize"> <size> <width>16777215</width> <height>52</height> </size> </property> <property name="windowTitle"> <string>Toast</string> </property> <layout class="QGridLayout" name="gridLayout"> <property name="leftMargin"> <number>0</number> </property> <property name="topMargin"> <number>0</number> </property> <property name="rightMargin"> <number>0</number> </property> <property name="bottomMargin"> <number>0</number> </property> <property name="spacing"> <number>0</number> </property> <item row="0" column="0"> <widget class="QLabel" name="label"> <property name="styleSheet"> <string notr="true">background-color: rgba(0,0,0,0.80); border-radius: 26px; color: #FFFFFF; font-family: microsoft yahei; font-size: 16px; padding-left:25px; padding-right:25px;</string> </property> <property name="text"> <string>TextLabel</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> <property name="indent"> <number>-1</number> </property> </widget> </item> </layout> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/> </ui>
×
×
  • 创建新的...

重要信息

注册必须使用2-8个中文汉字作为账号