qt 实现一个基于QLabel逐渐消隐的弹窗

#pragma once
#include <QLabel>
#include <QTimer>
#include <atomic>
using namespace std;

enum EnType
{
	enLabelOk =1,
	enLabelError
};

class YxCloseAnimationLabel:public QLabel
{
	Q_OBJECT
public:
	YxCloseAnimationLabel(QString _strText, EnType _type,QWidget *parent = Q_NULLPTR );
	~YxCloseAnimationLabel();
private slots:
	void   closeAnimation();
protected:
	void paintEvent(QPaintEvent *event);
private:
	QTimer	m_timer;
	QFont m_font;
	static	atomic_short m_iCount;
	EnType m_type;
};

#include "YxCloseAnimationLabel.h"
#include <QPainter>
#include <QPropertyAnimation>

atomic_short YxCloseAnimationLabel::m_iCount = 0;

YxCloseAnimationLabel::YxCloseAnimationLabel(QString _strText, EnType _type, QWidget *parent /*= Q_NULLPTR */):QLabel(parent)
,m_type(_type)
{
	if (m_iCount > 4) {
		m_iCount = 0;
	}
	
	setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
	//setAttribute(Qt::WA_TranslucentBackground);
	setAttribute(Qt::WA_QuitOnClose);
	setAttribute(Qt::WA_DeleteOnClose);
	m_font.setPointSize(10);
	m_font.setFamily("msyh");
	
	QFontMetrics metrics = QFontMetrics(m_font);
	setFixedSize(metrics.width(_strText) + 45,
		metrics.height() + 22);	
	QLabel::setText(_strText);
	connect(&m_timer, SIGNAL(timeout()), SLOT(closeAnimation()));
	m_timer.setInterval(1200);
	m_timer.start();
	m_iCount++;

}

YxCloseAnimationLabel::~YxCloseAnimationLabel()
{
}

void YxCloseAnimationLabel::closeAnimation()
{
	m_timer.stop();
	QPropertyAnimation *animation = new QPropertyAnimation(this, "windowOpacity", this);
	animation->setDuration(1300);
	animation->setStartValue(1);
	animation->setEndValue(0);
	animation->start();
	connect(animation, SIGNAL(finished()), SLOT(close()));
}
void YxCloseAnimationLabel::paintEvent(QPaintEvent *event) {
	QPainter painter(this);
	painter.setRenderHint(QPainter::Antialiasing);
	painter.setPen(Qt::NoPen);
	painter.setBrush(QColor(255, 255, 255));
	painter.drawRoundRect(rect(), 6, 6);
	painter.setFont(m_font);
	painter.setPen(QColor(89, 89, 89));
	QRect textRect;
	textRect.setRect(rect().x()+26, rect().y(), rect().width()- 26,rect().height());
	painter.drawText(textRect, Qt::AlignCenter, text());
	QRect windowsRect = geometry();
	int x = 10;
	int y = (windowsRect.height() - 16) / 2;
	QPoint imagePoint(x, y);
	QImage image_;
	if (m_type == enLabelOk)
	{
		image_.load(":/YxStreamClient/Resources/ok_ico.png");
	}
	else {
		image_.load(":/YxStreamClient/Resources/error_ico.png");
	}
	painter.drawImage(imagePoint, image_);
	painter.end();
	windowsRect.moveTop((windowsRect.height()*m_iCount + 10* m_iCount));
	setGeometry(windowsRect);
 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值