#include "Toastwidget.h"
#include <QHBoxLayout>
#include <QTimer>
#include <QGraphicsDropShadowEffect>
#include <QFontMetrics>
#include <QApplication>
#include <QDesktopWidget>
QMap<QString, QRect> ToastWidget::sToastMap;
QReadWriteLock ToastWidget::sLocker;
#define MAX_LEN 500
ToastWidget::ToastWidget(QWidget* parent) :
QWidget(parent)
{
setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground);
QWidget* centralWidget = new QWidget(this);
centralWidget->setStyleSheet("background-color: rgba(255, 255, 255, 0.3);border-radius:10px;");
QHBoxLayout* pLayout = new QHBoxLayout(this);
pLayout->addWidget(centralWidget);
pLayout->setContentsMargins(20, 20, 20, 20);
QHBoxLayout* horizontalLayout = new QHBoxLayout(centralWidget);
horizontalLayout->setContentsMargins(25, 15, 30, 15);
label_icon = new QLabel(centralWidget);
// label_icon->setMinimumSize(QSize(20, 20));
// label_icon->setMaximumSize(QSize(20, 20));
label_icon->setPixmap(QPixmap(":/Resources/Toast/toast_success.png"));
horizontalLayout->addWidget(label_icon);
label_text = new QLabel(centralWidget);
QFont font( "Microsoft YaHei", 13, 65);
label_text->setFont(font);
label_text->setAttribute(Qt::WA_TranslucentBackground);
horizontalLayout->addWidget(label_text);
QGraphicsDropShadowEffect* pEffect = new QGraphicsDropShadowEffect(centralWidget);
pEffect->setOffset(0, 0);
//阴影颜色
pEffect->setColor(QColor(89, 126, 247));
//阴影半径
pEffect->setBlurRadius(5);
centralWidget->setGraphicsEffect(pEffect);
}
ToastWidget::~ToastWidget()
{
if(m_animation != nullptr)
{
m_animation->stop();
m_animation->deleteLater();
m_animation = nullptr;
}
ToastWidget::removeToast(m_text);
}
void ToastWidget::setIcon(const QString& icon)
{
label_icon->setPixmap(QPixmap(icon).scaled(30,30));
}
void ToastWidget::setText(const QString& text)
{
m_text = text;
QString str = text;
QFontMetrics metrics(label_text->font());
if (metrics.width(m_text) > MAX_LEN)
{
str = QFontMetrics(label_text->font()).elidedText(m_text, Qt::ElideMiddle, MAX_LEN);
}
label_text->setText(str);
}
void ToastWidget::timeOutSlot()
{
if(m_animation != nullptr)
{
m_animation->stop();
delete m_animation;
m_animation = nullptr;
}
// 结束动画
m_animation = new QPropertyAnimation(this, "windowOpacity");
m_animation->setDuration(600);
m_animation->setStartValue(1);
m_animation->setEndValue(0);
m_animation->start();
connect(m_animation, &QPropertyAnimation::finished, [this]
{
close();
deleteLater();
});
}
void ToastWidget::showAnimation(int timeout)
{
if(m_animation != nullptr)
{
m_animation->stop();
delete m_animation;
m_animation = nullptr;
}
// 开始动画
m_animation = new QPropertyAnimation(this, "windowOpacity");
m_animation->setDuration(400);
m_animation->setStartValue(0);
m_animation->setEndValue(1);
m_animation->start();
show();
this->activateWindow();
this->raise();
QTimer::singleShot(timeout, this, &ToastWidget::timeOutSlot);
}
void ToastWidget::showTip(const QString& text, const QString& icon, QWidget* parent /*= nullptr*/)
{
if (ToastWidget::existToast(text))
{
return;
}
ToastWidget* toast = new ToastWidget(/*parent*/);
toast->setWindowFlags(toast->windowFlags() | Qt::WindowStaysOnTopHint); // 置顶
toast->setText(text);
toast->setIcon(icon);
toast->adjustSize();
QRect rc;
int x,y;
int w = toast->width();
int h = toast->height();
if (parent == nullptr)
{
QDesktopWidget *desktop = QApplication::desktop();
rc = QRect(0, 0, desktop->width(), desktop->height());
x = rc.width()/2 - w/2;
y = rc.height()/2 - h/2;
}
else
{
rc = parent->rect();
x = rc.width()/2 - w/2 + parent->x();
y = rc.height()/2 - h/2 + parent->y();
}
//if (parent == nullptr)
{
x += rc.left();
y += rc.top();
}
{
QReadLocker locker(&sLocker);
while (1)
{
QRect rc = QRect(x, y, toast->width(), toast->height());
rc = rc.marginsRemoved(QMargins(20, 20, 20, 20));
QMap<QString, QRect>::iterator it = sToastMap.begin();
bool intersects = false;
while( it != sToastMap.end())
{
if (it.value().intersects(rc))
{
intersects = true;
y += (toast->height() - 40 + 10);
break;
}
it++;
}
if (intersects == false)
{
break;
}
}
}
rc = QRect(x, y, toast->width(), toast->height());
rc = rc.marginsRemoved(QMargins(20, 20, 20, 20));
insertToast(text, rc);
toast->move(x, y);
toast->showAnimation();
}
void ToastWidget::showInformation(const QString& text, QWidget* parent)
{
ToastWidget::showTip(text,
":/Resources/Toast/toast_success.png",
parent);
}
void ToastWidget::showError(const QString& text, QWidget* parent)
{
ToastWidget::showTip(text,
":/Resources/Toast/toast_fail.png",
parent);
}
void ToastWidget::showWarning(const QString& text, QWidget* parent)
{
ToastWidget::showTip(text,
":/Resources/Toast/toast_warnning.png",
parent);
}
void ToastWidget::insertToast(const QString& text, QRect rect)
{
QWriteLocker locker(&sLocker);
sToastMap[text] = rect;
}
void ToastWidget::removeToast(const QString& text)
{
QWriteLocker locker(&sLocker);
sToastMap.remove(text);
}
bool ToastWidget::existToast(const QString& text)
{
QReadLocker locker(&sLocker);
return sToastMap.contains(text);
}
没有合适的资源?快使用搜索试试~ 我知道了~
Qt自定义消息提示框源码
共5个文件
png:3个
h:1个
cpp:1个
需积分: 0 0 下载量 60 浏览量
2024-02-20
16:03:05
上传
评论
收藏 5KB ZIP 举报
温馨提示
自带资源文件 还有cpp和h文件 使用的时候引用头文件并将资源图片添加到项目资源中即可,详细介绍请看http://t.csdnimg.cn/qYRrM 有大概的方法,将文章中缺少的方法进行了补全
资源推荐
资源详情
资源评论
收起资源包目录
ToastWidget.zip (5个子文件)
ToastWidget
Toastwidget.cpp 6KB
Resources
Toast
toast_fail.png 678B
toast_success.png 955B
toast_warnning.png 758B
Toastwidget.h 1KB
共 5 条
- 1
资源评论
辣死那个胖子
- 粉丝: 6
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功