天天看點

Qt實作移動端Toast提示消息

先上具體的實作效果圖:

Qt實作移動端Toast提示消息

彈出提示後,提示框在一定時間内消失。

程式

程式頭檔案定義:

/** @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();
}