一、功能与演示

消息框快进慢出,定时一段时间后消失

二、源码

Toast.h

#ifndef TOAST_H
#define TOAST_H#include <QLabel>
#include <QWidget>class Toast : public QWidget
{Q_OBJECT
public:explicit Toast(QWidget *parent = nullptr);~Toast();void setText(const QString &text);void showAnimation(int timeout = 2000);public:static void showTip(const QString &text, QWidget *parent = nullptr);static void showTip(const QString &text, int timeout, QWidget *parent = nullptr);protected:virtual void paintEvent(QPaintEvent *event);
};

Toast.cpp

#include "Toast.h"
#include <QGuiApplication>
#include <QPainter>
#include <QPropertyAnimation>
#include <QScreen>
#include <QTimer>
#include <QVBoxLayout>Toast::Toast(QWidget *parent): QWidget(parent)
{setWindowFlags(windowFlags() | Qt::FramelessWindowHint | Qt::Tool);setAttribute(Qt::WA_TranslucentBackground, true);QVBoxLayout *layout = new QVBoxLayout;QLabel *label = new QLabel(this);label->setObjectName("labelText");label->setStyleSheet("background-color: rgba(255,0,0,0.80);""border-radius: 16px;""color: #FFFFFF;""font-family: microsoft yahei;""font-size: 30px;""padding-left:25px;""padding-right:25px;");label ->setAlignment(Qt::AlignCenter);label->setWordWrap(true);layout->addWidget(label);setLayout(layout);
}Toast::~Toast()
{}void Toast::setText(const QString &text)
{QLabel *label = this->findChild<QLabel *>("labelText");label->setText(text);
}void Toast::showAnimation(int timeout)
{/* 快进慢出 */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)
{Toast *toast = new Toast(parent);toast->setWindowFlags(toast->windowFlags() | Qt::WindowStaysOnTopHint); /* 置顶 */toast->setText(text);toast->adjustSize();   QScreen *pScreen = QGuiApplication::primaryScreen();toast->move((pScreen->size().width() - toast->width()) / 2, pScreen->size().height() * 0.4);toast->showAnimation();
}void Toast::showTip(const QString &text, int timeout, QWidget *parent)
{Toast *toast = new Toast(parent);toast->setWindowFlags(toast->windowFlags() | Qt::WindowStaysOnTopHint);toast->setText(text);toast->adjustSize();   QScreen *pScreen = QGuiApplication::primaryScreen();toast->move((pScreen->size().width() - toast->width()) / 2, pScreen->size().height() * 0.4);toast->showAnimation(timeout);
}void Toast::paintEvent(QPaintEvent *event)
{Q_UNUSED(event)QPainter paint(this);auto kBackgroundColor = QColor(255, 255, 255);kBackgroundColor.setAlpha(0.0 * 255); paint.setPen(Qt::NoPen);paint.setBrush(QBrush(kBackgroundColor, Qt::SolidPattern)); paint.drawRect(0, 0, width(), height());
}

使用方式

Toast::showTip(tr("Camera disconnected"), 1000, nullptr);

【QT】自定义Toast消息提示相关推荐

  1. Qt QWidget实现消息提示控件TipsWidget

    前言 用Qt实现一个消息提示控件,控件宽度会根据显示的内容多少来动态伸展,控件显示三秒钟过后会自动渐变透明度然后最终消失,这有点类似于Android的Toast控件,都是用于消息短暂提示. 源码 头文 ...

  2. flutter Toast消息提示框

    题记 -- 执剑天涯,从你的点滴积累开始,所及之处,必精益求精,即是折腾每一天. 本文章将讲述: 1.在 flutter 跨平台开发中,使用 Dart 实现 Toast 消息提示框效果 2.Overl ...

  3. Toast 消息提示框

    Toast(吐丝框) 1.1 Toast是Android中的一种简易的消息提示框 1.2 使用这个类的最简单的方法是调用静态方法构造您所需要的一切,并返回一个新的Toast对象. 第一个参数:当前的上 ...

  4. 纯JS 实现 简易 Toast 消息提示框

    //提示信息 封装 function Toast(msg,duration){ duration=isNaN(duration)?3000:duration; var m = document.cre ...

  5. 华为触摸提示音怎么换_抖音苹果iPhone手机怎么改微信消息提示音 自定义换声音教程...

    [闽南网] 最近抖音上有个视频是把苹果手机的微信消息提示音修改成很可爱的声音.很多小伙伴都在问这个iPhone的微信消息提示音要怎么改,还不知道的朋友小编在下面给大家分享苹果iPhone手机更换自定义 ...

  6. qq撤回的信息腾讯服务器有吗,腾讯官方:撤回消息为何提示对方?丨QQ新增自定义撤回消息~...

    原标题:腾讯官方:撤回消息为何提示对方?丨QQ新增自定义撤回消息~ 网友"十大未解之谜"之一---撤回消息为什么还要提示对方?近日,腾讯官方终于给出了一个官方解释. 腾讯表示,在平 ...

  7. 安卓Toast显示提示消息(自定义view,根据子线程消息显示提示)

    全栈工程师开发手册 (作者:栾鹏) 安卓教程全解 安卓Toast显示提示消息. 使用系统自带Toast提示框 //显示一个Toast private void displayToast() {Cont ...

  8. Android_(消息提示)多种使用Toast的消息提示

    Toast是一种提供给用户简介信息的视图,可以创建和显示消息,该视图以浮于应用程序之上的形式呈现给用户.因为它并不获得焦点,即使用户正在输入什么也不会受到影响. Toast目标是尽可能以不显眼的方式, ...

  9. Toast类实现消息提示框

    Toast类实现消息提示框的方式有两种: 使用静态方法makeText()方法 以下面那个为例吧,第一个参数是一个上下文对象,第二个参数是要显示的数据,第三个参数是要显示数据的时长. 我们来看看他低层 ...

  10. 消息提示类控件使用之Toast(吐司)的简单使用

    (一)概述 Android用于提示信息的一个控件--Toast(吐司)!Toast是一种很方便的消息提示框,会在 屏幕中显示一个消息提示框,没任何按钮,也不会获得焦点一段时间过后自动消失! 非常常用! ...

最新文章

  1. shared_ptr 的使用及注意事项
  2. 变分模态分解_Android小部件示例中的模态对话框(弹出)
  3. 最优布线问题(克鲁斯卡尔)
  4. Linux下主DNS与辅助DNS的配置(上)
  5. 什么是 SAP Spartacus FacadeFactoryService 中的 Resolver
  6. 在线颜色拾取器 - 资源篇
  7. ROW_NUMBER (Transact-SQL)
  8. Java MyBatis 别名
  9. mysql 杂记(一)
  10. jquery绑定方法on的
  11. UnityParticle1:粒子系统简介
  12. 易语言静态连接器提取_vc98linker修复静态|易语言vc98linker静态连接器迷你版_最火软件站...
  13. 用C 语言实现斐波那契数列
  14. 微信小程序自动化测试——智能化 Monkey
  15. 手机淘宝自动加好友及聊天实现
  16. NotePad++安装HEX-Editor插件
  17. 零基础入门大数据工程师从底层到应用必备技术汇总
  18. (附源码)springboot在线学习网站 毕业设计 751841
  19. photo-sphere-viewer
  20. 蒙太奇效果 android,Effect Photo Editor app

热门文章

  1. java导出excel设置行高列宽_Java 设置Excel自适应行高、列宽
  2. MySQL使用order by默认是升序还是降序?
  3. 计算机控制系统的信号的特点,现场总线控制系统的特点和优点
  4. phpmyadmin 下载、安装、配置
  5. Apq.Threading.js
  6. Web模式使用RabbitMQ
  7. 从k-\epsilon到k-\omega
  8. win7安装php失败,win7打印机驱动安装失败怎么办
  9. Kindle——电子书格式转换(二)
  10. 电脑自动出现html文件,当前页面发生脚本错误 电脑总出现当前页面脚本错误怎么办?...