阅读 123

QT实现定时关闭消息提示框

这篇文章主要介绍了软件利用Qt简单实现消息提示框可定时自动关闭,文中的示例代码讲解详细,对我们;了解QT有一定的帮助,感兴趣的可以学习一下

目录
  • 一、简述

  • 二、效果

  • 三、工程结构

  • 四、源文件 

一、简述

使用Qt简单实现提示框可定时自动关闭。

例子打包:链接

二、效果

三、工程结构

UI界面

四、源文件 

NoticeWidget.pro文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
QT       += core gui
  
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  
TARGET = Notice
TEMPLATE = app
  
  
SOURCES += main.cpp\
        mainwindow.cpp \
    noticewidget.cpp
  
HEADERS  += mainwindow.h \
    noticewidget.h
  
FORMS    += mainwindow.ui

mainwindow.h文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
  
#include <QMainWindow>
  
namespace Ui {
class MainWindow;
}
  
class MainWindow : public QMainWindow
{
    Q_OBJECT
  
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
  
private slots:
    void on_pushButtonShowNotice_clicked();
  
private:
    Ui::MainWindow *ui;
};
  
#endif // MAINWINDOW_H

mainwindow.cpp文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "noticewidget.h"
  
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("定时自动关闭消息提示框");
    ui->plainTextEditMsg->setPlainText("定时自动关闭消息提示框测试,简单测试例子");
}
  
MainWindow::~MainWindow()
{
    delete ui;
}
  
void MainWindow::on_pushButtonShowNotice_clicked()
{
    static NoticeWidget noticeWin;
    noticeWin.Notice(this, ui->plainTextEditMsg->toPlainText(), 3000);
}

noticewidget.h文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#ifndef _NoticeWidget_H_
#define _NoticeWidget_H_
  
#include <QLabel>
#include <QTimer>
  
//定时器间隔,单位ms
#define TIMER_INTERVAL_MS   50
  
//默认提示时间1s
#define NOTICE_DEF_DELAY_CNT     (1000/TIMER_INTERVAL_MS)
  
//透明度最大值255,也就是不透明
#define TRANSPARENT_MAX_VAL 255
  
//透明度递减值
#define TRANSPARENT_CUT_VAL (TRANSPARENT_MAX_VAL/NOTICE_DEF_DELAY_CNT + 1)
  
//大小比例
#define SIZE_SCALE  0.8
  
//间距调整
#define PADDING     4
  
//样式,字体颜色:白色;圆角;背景色透明度
#define STYLE_SHEET "color:white;border-radius:8px;background-color:rgba(80, 80, 80, %1);"
  
class NoticeWidget :public QLabel
{
    Q_OBJECT
  
public:
    void Notice(QWidget *parent, const QString &msg, const int delay_ms = 2000);
  
public:
    explicit NoticeWidget(QWidget *parent = 0);
    ~NoticeWidget();
  
private:
    void SetMesseage(const QString &msg, int delay_ms);
    void ChangeSize();
  
public slots:
    void OnTimerTimeout();
  
private:
    QWidget *mParentPtr;
    QTimer  *mTimerPtr;
    int mTimerCount;
    int mBaseWidth;  //按一行时算的宽度
    int mBaseHeight; //一行高度
    int mTransparentVal;//透明度0~255,值越小越透明
};
  
#endif // _NoticeWidget_H_

noticewidget.cpp文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "noticewidget.h"
  
NoticeWidget::NoticeWidget(QWidget *parent)
    : mParentPtr(parent)
    , mTimerPtr(nullptr)
    , mTimerCount(NOTICE_DEF_DELAY_CNT)
    , mBaseWidth(0)
    , mBaseHeight(0)
    , mTransparentVal(TRANSPARENT_MAX_VAL)
  
{
    //文字居中
    setAlignment(Qt::AlignCenter);
  
    //定时器,定时消失
    mTimerPtr = new QTimer(this);
    connect(mTimerPtr, SIGNAL(timeout()), this, SLOT(OnTimerTimeout()), Qt::UniqueConnection);
}
  
NoticeWidget::~NoticeWidget()
{
    if (mTimerPtr->isActive()) {
        mTimerPtr->stop();
    }
    deleteLater();
}
  
void NoticeWidget::OnTimerTimeout()
{
    --mTimerCount;
    if (0 < mTimerCount) {
        //重新定位(窗口大小和位置可能变化)
        if (nullptr != mParentPtr) {
            QPoint pt((mParentPtr->width() - width()) >> 1, (mParentPtr->height() - height()) >> 1);
            if (pos() != pt) {//父窗口位置变化
                ChangeSize();
                move(pt);
            }
        }
        //最后1s开始渐变消失
        if (mTimerCount <= NOTICE_DEF_DELAY_CNT && 0 < mTransparentVal) {
            mTransparentVal -= TRANSPARENT_CUT_VAL;
            if (0 > mTransparentVal) {
                mTransparentVal = 0;
            }
            //控制透明度
            setStyleSheet(QString(STYLE_SHEET).arg(mTransparentVal));
        }
    } else {//显示结束
        mTimerPtr->stop();
        setVisible(false);       
    }
}
  
//设置要显示的消息
void NoticeWidget::SetMesseage(const QString &msg, int delay_ms)
{
    mParentPtr = parentWidget();
  
    QFontMetrics fontMetrics(font());
    mBaseWidth = fontMetrics.width(msg);
    mBaseHeight = fontMetrics.height() + PADDING;
  
    //设置宽高
    ChangeSize();
  
    //换行
    setWordWrap(true);
  
    //设置显示内容
    setText(msg);
  
    //居中
    if (nullptr != mParentPtr) {
        move((mParentPtr->width() - width()) >> 1, (mParentPtr->height() - height()) >> 1);
    }
  
    setVisible(true);//显示
    setStyleSheet(QString(STYLE_SHEET).arg(TRANSPARENT_MAX_VAL));//设置样式,不透明
    mTimerCount = delay_ms/TIMER_INTERVAL_MS + 1;//延时计数计算
    mTransparentVal = TRANSPARENT_MAX_VAL;
}
  
//跟随父窗口大小变化
void NoticeWidget::ChangeSize()
{
    if (nullptr != mParentPtr) {
        double wd = mParentPtr->width() * SIZE_SCALE;//宽度占父窗口的80%
        setFixedSize((int)wd, mBaseHeight*(mBaseWidth/wd + 1));
    }
}
  
//显示消息,可通过设置delay_ms=0来立即关闭显示
void NoticeWidget::Notice(QWidget *parent, const QString &msg, const int delay_ms)
{
    if (mTimerPtr->isActive()) {
        mTimerPtr->stop();
        setVisible(false);
    }
  
    //消息为空直接返回
    if (msg.isEmpty() || 0 >= delay_ms) {
        return;
    }
  
    setParent(parent);
    SetMesseage(msg, delay_ms);
    mTimerPtr->start(TIMER_INTERVAL_MS);//开始计数
}

main.cpp文件

1
2
3
4
5
6
7
8
9
10
11
#include "mainwindow.h"
#include <QApplication>
  
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
  
    return a.exec();
}

到此这篇关于QT实现定时关闭消息提示框的文章就介绍到这了

原文链接:https://blog.csdn.net/nanfeibuyi/article/details/122291656


文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐