zl程序教程

您现在的位置是:首页 >  工具

当前栏目

Qt实现移动端Toast提示消息

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