Qt实现移动端Toast提示消息
2023-09-14 09:07:01 时间
先上具体的实现效果图:
弹出提示后,提示框在一定时间内消失。
程序
程序头文件定义:
/** @file Toast.h
* @brief Qt模拟安卓移动客户端Toast提示消息
* @note qss set in ui file
* @author lesliefish
* @date 2019/05/31
*/
#pragma once
#include <QtWidgets/QWidget>
#include "ui_Toast.h"
class Toast : public QWidget
{
Q_OBJECT
public:
Toast(QWidget *parent = Q_NULLPTR);
~Toast();
void setText(const QString& text);
void showAnimation(int timeout = 2000);// 动画方式show出,默认2秒后消失
public:
// 静态调用
static void showTip(const QString& text, QWidget* parent = nullptr);
protected:
virtual void paintEvent(QPaintEvent *event);
private:
Ui::ToastClass ui;
};
cpp文件:
#include "Toast.h"
#include <QPropertyAnimation>
#include <QScreen>
#include <QGuiApplication>
#include <QPainter>
#include <QTimer>
Toast::Toast(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
setWindowFlags(windowFlags() | Qt::FramelessWindowHint | Qt::Tool);// 无边框 无任务栏
setAttribute(Qt::WA_TranslucentBackground, true); // 背景透明
}
Toast::~Toast()
{
}
void Toast::setText(const QString& text)
{
ui.label->setText(text);
}
void Toast::showAnimation(int timeout /*= 2000*/)
{
// 开始动画
QPropertyAnimation *animation = new QPropertyAnimation(this, "windowOpacity");
animation->setDuration(1000);
animation->setStartValue(0);
animation->setEndValue(1);
animation->start();
show();
QTimer::singleShot(timeout, [&]
{
// 结束动画
QPropertyAnimation *animation = new QPropertyAnimation(this, "windowOpacity");
animation->setDuration(1000);
animation->setStartValue(1);
animation->setEndValue(0);
animation->start();
connect(animation, &QPropertyAnimation::finished, [&]
{
close();
deleteLater();// 关闭后析构
});
});
}
void Toast::showTip(const QString& text, QWidget* parent /*= nullptr*/)
{
Toast* toast = new Toast(parent);
toast->setWindowFlags(toast->windowFlags() | Qt::WindowStaysOnTopHint); // 置顶
toast->setText(text);
toast->adjustSize(); //设置完文本后调整下大小
// 测试显示位于主屏的70%高度位置
QScreen* pScreen = QGuiApplication::primaryScreen();
toast->move((pScreen->size().width() - toast->width()) / 2, pScreen->size().height() * 7 / 10);
toast->showAnimation();
}
void Toast::paintEvent(QPaintEvent *event)
{
QPainter paint(this);
paint.begin(this);
auto kBackgroundColor = QColor(255, 255, 255);
kBackgroundColor.setAlpha(0.0 * 255);// 透明度为0
paint.setRenderHint(QPainter::Antialiasing, true);
paint.setPen(Qt::NoPen);
paint.setBrush(QBrush(kBackgroundColor, Qt::SolidPattern));//设置画刷形式
paint.drawRect(0, 0, width(), height());
paint.end();
}
测试程序代码:
#include "Toast.h"
#include <QtWidgets/QApplication>
#include <QTimer>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QStringList texts{};
texts << QStringLiteral("真理惟一可靠的标准就是永远自相符合。")
<< QStringLiteral("Saying and doing are two different things.")
<< QStringLiteral("Two heads are better than one.")
<< QStringLiteral("Time flies.")
<< QStringLiteral("勿谓言之不预!")
<< QStringLiteral("Good company on the road is the shortest cut.")
<< QStringLiteral("Time to go.")
<< QStringLiteral("It is never too late to learn.")
<< QStringLiteral("贸易战中国必胜!");
QTimer timer;
QObject::connect(&timer, &QTimer::timeout, [&]
{
static int i = 0;
Toast::showTip(texts[i%texts.size()], nullptr);
i++;
});
Toast::showTip(QString("Let's go."), nullptr);
timer.start(4000);
return app.exec();
}
完整工程源码路径
环境:vs2015+Qt5.9.6
https://github.com/lesliefish/Qt/tree/master/UI/Toast
相关文章
- Qt-自定义可编辑模型
- Qt自绘系列-频谱图
- Linux系统使用Qt开发快速入门教程(linux下qt教程)
- 实现Linux下Qt应用静态编译的方法(linux下qt静态编译)
- Linux编译Qt库:从零开始(linux编译qt库)
- 使用 Qt 连接 MySql (qt使用mysql)
- 快速搭建 Qt 与 MySQL 的数据库应用(qt使用mysql)
- 探究ARM架构下基于Linux系统的QT框架应用(armlinuxqt)
- linux系统启动Qt图形界面简易指南(linux启动qt)
- 从零开始:Qt 编译在 Linux 上的指南(qt编译linux)
- 如何在QT应用程序中使用MYSQL进行编程?(qt的mysql编程)
- 配置Linux的QT环境变量,轻松打造高效开发环境(qt环境变量linux)
- Linux Qt窗口:快速构建你的GUI界面(linux qt 窗口)
- QT快速连接MSSQL数据库,轻松搞定!(QT链接mssql)
- 轻松实现QT直连Microsoft SQL Server(qt直连mssql)
- 借助Qt实现向MSSQL中快速插入数据(qt向mssql插入数据)
- Linux下Qt音频播放:轻松享受音乐乐活力(linux下qt音频播放)
- Redis与Qt实现完美连接(redis连接qt)