Qt实现移动端Toast提示消息

590 篇文章 10 订阅 ¥119.90 ¥99.00

先上具体的实现效果图:
在这里插入图片描述
弹出提示后,提示框在一定时间内消失。

程序

程序头文件定义:

/** @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

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值