实现的功能:
弹出一个悬浮在屏幕上的对话框,延时一定时间后自动消失。
效果图:
使用方法:
1、将toast.cpp和toast.h添加到QT工程
2、需要使用的地方包含头文件
#include "toast.h"
3、代码调用显示:
Toast::instance().show(Toast::INFO, "this is INFO!");
Toast::instance().show(Toast::WARN, "this is WARN!");
Toast::instance().show(Toast::ERROR, "this is ERROR!");
源码:
note:
代码是从一套开源代码中抠出来的,链接在底部。
实现逻辑也比较简单,大家可以研究一下。
代码可移植性比较强,显示效果可以根据需要自己优化。再次感谢原作者!!
toast.cpp
#include "toast.h"
#include <QDialog>
#include <QHBoxLayout>
#include <QLabel>
#include <QEvent>
class ToastDlg: public QDialog
{
private:
QLabel* mLabel;
QLabel* mCloseBtn;
protected:
bool eventFilter(QObject *obj, QEvent *ev) override
{
if (obj == mCloseBtn)
{
if (ev->type() == QEvent::MouseButtonRelease)
{
accept();
}
}
return QObject::eventFilter(obj, ev);
}
public:
ToastDlg()
{
auto layout = new QHBoxLayout;//水平布局
mLabel = new QLabel;
mLabel->setStyleSheet("color: white; background:transparent");//red
layout->addWidget(mLabel, 1);//stretch = 1
mCloseBtn = new QLabel;
//mCloseBtn->setPixmap(QPixmap(":/res/img/close.png"));
mCloseBtn->installEventFilter(this);
mCloseBtn->setStyleSheet("background:transparent");
layout->addWidget(mCloseBtn);
setLayout(layout);
setWindowFlag(Qt::FramelessWindowHint);//生成一个无边界窗口。用户不能通过窗口系统移动或调整无边界窗口的大小。
setAttribute(Qt::WA_ShowWithoutActivating,true);//Show the widget without making it active.
//setAttribute(Qt::WA_TranslucentBackground, true); // 背景透明
}
void show(Toast::Level level, const QString& text)
{
QPalette p = palette();
//QColor(int r, int g, int b, int a = 255)
//Constructs a color with the RGB value r, g, b, and the alpha-channel (transparency) value of a.
p.setColor(QPalette::Window, QColor(0,0,0,200));
if (level == Toast::INFO)
{
p.setColor(QPalette::Window, QColor(0,0,0,200));//黑色
}
else if (level == Toast::WARN)
{
p.setColor(QPalette::Window, QColor(0,0,255,200));
}
else//ERROR
{
p.setColor(QPalette::Window, QColor(255,0,0,200));
}
setPalette(p);//set widget's palette
mLabel->setText(text);
setWindowFlag(Qt::WindowStaysOnTopHint);//通知窗口系统该窗口应保持在所有其他窗口的顶部。
QDialog::show();
}
};//~class ToastDlg end
Toast::Toast()//构造函数
{
mDlg = new ToastDlg;
}
//返回一个实例(instance)
Toast &Toast::instance()
{
static Toast thiz;//这种实例化方法会自动回收内存
return thiz;
}
void Toast::show(Toast::Level level, const QString &text)
{
mDlg->show(level, text);//ToastDlg.show方法
if (mTimerId != 0)//int mTimerId
{
//如果之前已经开启了一个定时器,先把他关掉
killTimer(mTimerId);
}
mTimerId = startTimer(2000);//启动定时器,每2s触发定时器事件,直到调用killTimer
}
//重写定时器事件回调函数
void Toast::timerEvent(QTimerEvent *event)
{
killTimer(mTimerId);
mTimerId = 0;
mDlg->accept();//隐藏模态对话框
//mDlg->hide();
}
toast.h
#ifndef TOAST_H
#define TOAST_H
#include <QObject>
#include <QRect>
class ToastDlg;
class Toast: QObject
{
public:
enum Level
{
INFO, WARN, ERROR
};
private:
Toast();
public:
static Toast& instance();
public:
void show(Level level, const QString& text);
private:
void timerEvent(QTimerEvent *event) override;
private:
ToastDlg* mDlg;
int mTimerId{0};
QRect mGeometry;
};
#endif // TOAST_H
ref:
https://blog.csdn.net/y396397735/article/details/90714253