QT动画之消息推送框
这几天项目中用到了做一个消息提示的动画,这里写了一个小测试Demo,先看下效果:
下面直接上代码:
#ifndef CHNSYSLAWTIPWIDGET_H
#define CHNSYSLAWTIPWIDGET_H
#include <QPropertyAnimation>
#include <QTextEdit>
#include <QTimer>
#include <QWidget>
class ChnsysLawTipWidget : public QWidget
{
Q_OBJECT
public:
explicit ChnsysLawTipWidget(QWidget *parent = nullptr);
//传入消息
void receiveMesage(QString msgstr);
public slots:
void moveOut();//移出
void moveIn();//飞入
void slot_startTime();
protected:
void showEvent(QShowEvent *event);
private:
void initializeUI();
void initializeAnimal();
private:
QTextEdit * m_pMessage;
QWidget * m_pMoveWidget;
QPropertyAnimation * m_pAnimation; //移除动画
QPropertyAnimation * m_pAnimationIn; //移入动画
QTimer *m_timer;
int m_DurationTime;
};
#endif // CHNSYSLAWTIPWIDGET_H
这里定义一个消息提示框类,这是头文件。
#include "chnsyslawtipwidget.h"
#include <QTimer>
#include <qboxlayout.h>
#include <qDebug>
ChnsysLawTipWidget::ChnsysLawTipWidget(QWidget *parent) : QWidget(parent)
,m_DurationTime(700)
{
initializeUI();
initializeAnimal();
}
void ChnsysLawTipWidget::receiveMesage(QString msgstr)
{
m_pMessage->setPlainText(msgstr);
//显示消息
moveIn();
this->show();
}
void ChnsysLawTipWidget::showEvent(QShowEvent *event)
{
QWidget::showEvent(event);
}
void ChnsysLawTipWidget::initializeUI()
{
m_pMessage = new QTextEdit(this) ;
m_pMessage->setEnabled(false);
m_pMoveWidget = new QWidget;
m_pMoveWidget->setStyleSheet("background-color:white");
QHBoxLayout *pLayout = new QHBoxLayout;
pLayout->setContentsMargins(0, 0, 0, 0);
pLayout->setSpacing(10);
pLayout->addWidget(m_pMessage);
m_pMoveWidget->setLayout(pLayout);
QHBoxLayout * bgLayout = new QHBoxLayout;
bgLayout->setSpacing(0);
bgLayout->setMargin(0);
bgLayout->addWidget(m_pMoveWidget);
this->setLayout(bgLayout);
m_pMoveWidget->move(-width(),0);
}
void ChnsysLawTipWidget::initializeAnimal()
{
m_pAnimation = new QPropertyAnimation(this);
m_pAnimationIn = new QPropertyAnimation(this);
m_timer = new QTimer(this);
//设置动画时间
m_pAnimation->setDuration(m_DurationTime);
m_pAnimationIn->setDuration(m_DurationTime);
connect(m_pAnimationIn, SIGNAL(finished()), this, SLOT(slot_startTime()));
connect(m_timer, SIGNAL(timeout()), this, SLOT(moveOut()));
//设置只响应一次
m_timer->setSingleShot(true);
//结束关闭上层底层窗体
connect(m_pAnimation, &QPropertyAnimation::finished, this, &ChnsysLawTipWidget::close);
}
void ChnsysLawTipWidget::moveOut()
{
m_pAnimation->setTargetObject(m_pMoveWidget);
m_pAnimation->setPropertyName("pos");
//设置开始结束位置
m_pAnimation->setStartValue(QPoint());
m_pAnimation->setEndValue(QPoint(-width(), 0));
m_pAnimation->start();
}
void ChnsysLawTipWidget::moveIn()
{
m_pAnimationIn->setTargetObject(m_pMoveWidget);
m_pAnimationIn->setPropertyName("pos");
m_pAnimationIn->setStartValue(QPoint(-width(), 0));
m_pAnimationIn->setEndValue(QPoint(0, 0));
m_pAnimationIn->start();
}
void ChnsysLawTipWidget::slot_startTime()
{
qDebug()<<"slot_startTime:"<<2222;
//设置消息框停留时常
m_timer->start(m_DurationTime*4);
}
initializeUI初始化窗体布局,窗体中放了一个QTextEdit来显示信息,封一层窗体,下层窗体透明显示;initializeAnimal 初始化动画,连接信号,移入动画结束,开始定时器,设置停留时间,定时器结束,开启移除动画,移除动画结束,关闭底层窗体,receiveMesage函数中 设置显示消息内容。也可以自己改开始结束位置设置从下往上弹窗。
完整源码地址:https://download.csdn.net/download/qq_25532071/11013126