Qt自定义MessageToast

效果:

文字长度自适应,自动居中到parent,会透明渐变消失。

cpp 复制代码
CustomToast::MessageToast(QS("最多添加50张图片"),this);

1. CustomToast.h

cpp 复制代码
#pragma once


#include <QFrame>

class CustomToast : public QFrame {
	Q_OBJECT
public:

	static void MessageToast(const QString &text, QWidget *parent = nullptr, int timeout = 1500);

private:

	CustomToast(QWidget *parent = nullptr, int timeout = 1500);

	void setText(const QString &text);
private:

	class Impl;
	std::shared_ptr<Impl> m_impl = nullptr;
	class Ui;
	std::shared_ptr<Ui> ui = nullptr;
};

2. CustomToast.cpp

cpp 复制代码
#include "CustomToast.h"

#include <QGraphicsOpacityEffect>
#include <QHBoxLayout>
#include <QLabel>
#include <QPropertyAnimation>
#include <QTimer>

#define STR(arg) #arg

class CustomToast::Ui {

public:
	void setupUi(QWidget *parent)
	{
		layout = new QHBoxLayout(parent);
		parent->setLayout(layout);
		parent->setContentsMargins(0, 0, 0, 0);
		layout->setContentsMargins(0, 0, 0, 0);

		layout->setContentsMargins(16, 8, 16, 8);

		labelIcon = new QLabel(parent);
		labelText = new QLabel(parent);


		auto iconLayout = new QHBoxLayout(parent);
		iconLayout->addWidget(labelIcon);
		iconLayout->setContentsMargins(0, 2, 0, 2);

		layout->addLayout(iconLayout);
		layout->addWidget(labelText);
		layout->setSpacing(3);

		labelIcon->setFixedSize(16, 16);
		labelText->setMinimumHeight(20);

		labelText->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
		parent->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);

		parent->setObjectName(STR(CustomToast));
		labelIcon->setObjectName(STR(labelIcon));
		labelText->setObjectName(STR(labelText));
	}

	QHBoxLayout *layout = nullptr;
	QLabel *labelIcon	= nullptr;
	QLabel *labelText	= nullptr;
};

class CustomToast::Impl {
public:
	Impl(CustomToast *parent, int timeout) : m_parent(parent), m_timeout(timeout)
	{
	}

	void startOpacityAnimation()
	{
		auto graphicsOpacityEffect = new QGraphicsOpacityEffect(m_parent);
		graphicsOpacityEffect->setOpacity(1.0);
		m_parent->setGraphicsEffect(graphicsOpacityEffect);

		auto opacityAnimation = new QPropertyAnimation(graphicsOpacityEffect, "opacity");
		opacityAnimation->setDuration(m_timeout);
		opacityAnimation->setStartValue(1.0);
		opacityAnimation->setEndValue(0);
		opacityAnimation->setEasingCurve(QEasingCurve::InCubic);
		opacityAnimation->start();
	}

	void centerToParent()
	{
		auto grandParent = m_parent->parentWidget();
		if (grandParent) {
			m_parent->move(
				grandParent->width() / 2 - m_parent->width() / 2, grandParent->height() / 2 - m_parent->height() / 2
			);
		}
	}

	void deleteLater()
	{
		QTimer::singleShot(m_timeout, m_parent, [=] { m_parent->deleteLater(); });
	}

private:
	QWidget *m_parent = nullptr;
	int m_timeout	  = 1500;
};

void
CustomToast::MessageToast(const QString &text, QWidget *parent /*= nullptr*/, int timeout /*= 1500*/)
{
	auto toast = new CustomToast(parent, timeout);
	toast->setText(text);
	toast->show();
	m_impl->centerToParent();
}

CustomToast::CustomToast(QWidget *parent, int timeout) :
	QFrame(parent), ui(std::make_shared<Ui>()), m_impl(std::make_shared<Impl>(this, timeout))
{
	ui->setupUi(this);
	
	m_impl->centerToParent();
	m_impl->startOpacityAnimation();
	m_impl->deleteLater();
}

void
CustomToast::setText(const QString &text)
{
	ui->labelText->setText(text);
	adjustSize();
}

3. 样式表:

css 复制代码
CustomToast{
    background: rgba(0, 0, 0, 0.8);
    border-radius: 4px;
}
CustomToast #labelText{
    color: #FFFFFF;
    font-family: Microsoft YaHei;
    font-size: 14px;
    font-weight: 400;
}
CustomToast #labelIcon{
    border-image: url(:/img/toast/info.png);
}
相关推荐
molunnnn17 小时前
第四章 Agent的几种经典范式
开发语言·python
洛_尘18 小时前
JAVA EE初阶 2: 多线程-初阶
java·开发语言
@卞18 小时前
C语言常见概念
c语言·开发语言
wjs202419 小时前
Eclipse 关闭项目详解
开发语言
沐知全栈开发19 小时前
《隐藏(Hide)》
开发语言
lkbhua莱克瓦2419 小时前
Java基础——方法
java·开发语言·笔记·github·学习方法
catchadmin19 小时前
PHP 依赖管理器 Composer 2.9 发布
开发语言·php·composer
范纹杉想快点毕业20 小时前
《嵌入式开发硬核指南:91问一次讲透底层到架构》
java·开发语言·数据库·单片机·嵌入式硬件·mongodb
毕设源码-邱学长20 小时前
【开题答辩全过程】以 基于Python的Bilibili平台数据分析与可视化实现为例,包含答辩的问题和答案
开发语言·python·数据分析