QT實(shí)現(xiàn)定時(shí)關(guān)閉消息提示框
一、簡(jiǎn)述
使用Qt簡(jiǎn)單實(shí)現(xiàn)提示框可定時(shí)自動(dòng)關(guān)閉。
例子打包:鏈接
二、效果
三、工程結(jié)構(gòu)
UI界面
四、源文件
NoticeWidget.pro文件
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文件
#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文件
#include "mainwindow.h" #include "ui_mainwindow.h" #include "noticewidget.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); setWindowTitle("定時(shí)自動(dòng)關(guān)閉消息提示框"); ui->plainTextEditMsg->setPlainText("定時(shí)自動(dòng)關(guān)閉消息提示框測(cè)試,簡(jiǎn)單測(cè)試?yán)?); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButtonShowNotice_clicked() { static NoticeWidget noticeWin; noticeWin.Notice(this, ui->plainTextEditMsg->toPlainText(), 3000); }
noticewidget.h文件
#ifndef _NoticeWidget_H_ #define _NoticeWidget_H_ #include <QLabel> #include <QTimer> //定時(shí)器間隔,單位ms #define TIMER_INTERVAL_MS 50 //默認(rèn)提示時(shí)間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 //間距調(diào)整 #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; //按一行時(shí)算的寬度 int mBaseHeight; //一行高度 int mTransparentVal;//透明度0~255,值越小越透明 }; #endif // _NoticeWidget_H_
noticewidget.cpp文件
#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); //定時(shí)器,定時(shí)消失 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 {//顯示結(jié)束 mTimerPtr->stop(); setVisible(false); } } //設(shè)置要顯示的消息 void NoticeWidget::SetMesseage(const QString &msg, int delay_ms) { mParentPtr = parentWidget(); QFontMetrics fontMetrics(font()); mBaseWidth = fontMetrics.width(msg); mBaseHeight = fontMetrics.height() + PADDING; //設(shè)置寬高 ChangeSize(); //換行 setWordWrap(true); //設(shè)置顯示內(nèi)容 setText(msg); //居中 if (nullptr != mParentPtr) { move((mParentPtr->width() - width()) >> 1, (mParentPtr->height() - height()) >> 1); } setVisible(true);//顯示 setStyleSheet(QString(STYLE_SHEET).arg(TRANSPARENT_MAX_VAL));//設(shè)置樣式,不透明 mTimerCount = delay_ms/TIMER_INTERVAL_MS + 1;//延時(shí)計(jì)數(shù)計(jì)算 mTransparentVal = TRANSPARENT_MAX_VAL; } //跟隨父窗口大小變化 void NoticeWidget::ChangeSize() { if (nullptr != mParentPtr) { double wd = mParentPtr->width() * SIZE_SCALE;//寬度占父窗口的80% setFixedSize((int)wd, mBaseHeight*(mBaseWidth/wd + 1)); } } //顯示消息,可通過設(shè)置delay_ms=0來立即關(guān)閉顯示 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);//開始計(jì)數(shù) }
main.cpp文件
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
到此這篇關(guān)于QT實(shí)現(xiàn)定時(shí)關(guān)閉消息提示框的文章就介紹到這了,更多相關(guān)QT定時(shí)關(guān)閉消息提示框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談十進(jìn)制小數(shù)和二進(jìn)制小數(shù)之間的轉(zhuǎn)換
下面小編就為大家?guī)硪黄獪\談十進(jìn)制小數(shù)和二進(jìn)制小數(shù)之間的轉(zhuǎn)換。小編覺得挺不錯(cuò)的現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01Visual Studio Community 2022(VS2022)安裝圖文方法
這篇文章主要介紹了Visual Studio Community 2022(VS2022)安裝方法,本文分步驟通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09VC++實(shí)現(xiàn)的OpenGL線性漸變色繪制操作示例
這篇文章主要介紹了VC++實(shí)現(xiàn)的OpenGL線性漸變色繪制操作,結(jié)合實(shí)例形式分析了VC++基于OpenGL進(jìn)行圖形繪制的相關(guān)操作技巧,需要的朋友可以參考下2017-07-07C++實(shí)現(xiàn)LeetCode(118.楊輝三角)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(118.楊輝三角),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07C/C++ 獲取Windows系統(tǒng)的位數(shù)32位或64位的實(shí)現(xiàn)代碼
這篇文章主要介紹了C/C++ 獲取Windows系統(tǒng)的位數(shù)32位或64位的實(shí)現(xiàn)代碼的相關(guān)資料,希望通過本文能幫助到大家,讓大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10