Qt实现右下角消息弹窗

文章目录

  • 一,思路
  • 二 ,实现

一,思路

右下弹窗实现思路:
1:制作一个需要显示的Qwidget窗口,将它移动到屏幕的右下角,且Qwidget的坐标位置为屏幕下边缘,这样就可以隐藏窗口。
2:使用qt动画,让Qwidget窗口从右下角边缘向上移动,直到Qwidget一整个显示出来。

二 ,实现

#include 
#include 
#include 
#include 
#include 
#include 
#include "ui_popupwindow.h"

class CPopUPWindow : public QDialog
{
	Q_OBJECT

public:
	CPopUPWindow(QWidget *parent = Q_NULLPTR);
	~CPopUPWindow();
	void showAnimation();
	void addNewMessage(QString str);
private slots:
	void closeAnimation();
	void clearAll();
signals:
	void sigFinished();
private:
	Ui::CPopUPWindow ui;
	QDesktopWidget			m_desktop;
	QPropertyAnimation*		m_animation;
	QTimer*					m_remainTimer;
};```

```cpp
#include "popupwindow.h"

CPopUPWindow::CPopUPWindow(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
	this->move((m_desktop.availableGeometry().width() - this->width()), m_desktop.availableGeometry().height());//初始化位置到右下角
	ui.textEdit->setStyleSheet("background-color:transparent;border: none;");
	QFont t_font("微软雅黑", 12);
	ui.textEdit->setFont(t_font);
	initTitleBar();
	//显示弹出框动画
	m_animation = new QPropertyAnimation(this, "pos");//动画属性名按点移动
	m_animation->setDuration(2000);	//设置弹出框显示2秒、在弹回去
	m_animation->setStartValue(QPoint(this->x(), this->y()));
	m_animation->setEndValue(QPoint((m_desktop.availableGeometry().width() - this->width()), (m_desktop.availableGeometry().height() - this->height())));
	m_remainTimer = new QTimer();
	connect(m_animation, SIGNAL(finished()), this, SLOT(clearAll()));
	connect(m_remainTimer, SIGNAL(timeout()), this, SLOT(closeAnimation()));
}

CPopUPWindow::~CPopUPWindow()
{
	if (m_animation != nullptr){
		delete m_animation;
		m_animation = nullptr;
	}
	if (m_remainTimer != nullptr){
		delete m_remainTimer;
		m_remainTimer = nullptr;
	}
	if (m_titleBar != nullptr){
		delete m_titleBar;
		m_titleBar = nullptr;
	}
	emit sigFinished();
}


void CPopUPWindow::showAnimation()
{
	if (m_animation->state() == QAbstractAnimation::Running){
		return;
	}
	m_animation->start();
	if (!m_remainTimer->isActive()){
		ui.textEdit->clear();
		m_remainTimer->start(4000);//弹出动画2S,停留2S回去
	}
}

void CPopUPWindow::closeAnimation()
{
	//清除Timer指针和信号槽
	m_remainTimer->stop();
	//弹出框回去动画
	m_animation->setStartValue(QPoint(this->x(), this->y()));
	m_animation->setEndValue(QPoint((m_desktop.availableGeometry().width() - this->width()), m_desktop.availableGeometry().height()));
	m_animation->start();
}

void CPopUPWindow::clearAll()
{
	closeAnimation();
	hide();
}

void CPopUPWindow::addNewMessage(QString str)
{
	QString t_str = QString::fromLocal8Bit("震源车%1上线").arg(str);
	ui.textEdit->append(t_str);
}

你可能感兴趣的:(燃犀的QT笔记,qt,开发语言)