欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

QT實(shí)現(xiàn)定時(shí)關(guān)閉消息提示框

 更新時(shí)間:2022年01月05日 11:54:55   作者:Genven_Liang  
這篇文章主要介紹了軟件利用Qt簡(jiǎn)單實(shí)現(xiàn)消息提示框可定時(shí)自動(dòng)關(guān)閉,文中的示例代碼講解詳細(xì),對(duì)我們;了解QT有一定的幫助,感興趣的可以學(xué)習(xí)一下

一、簡(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)文章

  • C語言新建臨時(shí)文件和臨時(shí)文件名的方法

    C語言新建臨時(shí)文件和臨時(shí)文件名的方法

    這篇文章主要介紹了C語言新建臨時(shí)文件和臨時(shí)文件名的方法,分別是mkstemp()函數(shù)和mktemp()函數(shù)的使用,需要的朋友可以參考下
    2015-08-08
  • 淺談十進(jìn)制小數(shù)和二進(jìn)制小數(shù)之間的轉(zhuǎn)換

    淺談十進(jìn)制小數(shù)和二進(jìn)制小數(shù)之間的轉(zhuǎn)換

    下面小編就為大家?guī)硪黄獪\談十進(jìn)制小數(shù)和二進(jìn)制小數(shù)之間的轉(zhuǎn)換。小編覺得挺不錯(cuò)的現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-01-01
  • Visual Studio Community 2022(VS2022)安裝圖文方法

    Visual Studio Community 2022(VS2022)安裝圖文方法

    這篇文章主要介紹了Visual Studio Community 2022(VS2022)安裝方法,本文分步驟通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-09-09
  • VC++實(shí)現(xiàn)的OpenGL線性漸變色繪制操作示例

    VC++實(shí)現(xiàn)的OpenGL線性漸變色繪制操作示例

    這篇文章主要介紹了VC++實(shí)現(xiàn)的OpenGL線性漸變色繪制操作,結(jié)合實(shí)例形式分析了VC++基于OpenGL進(jìn)行圖形繪制的相關(guān)操作技巧,需要的朋友可以參考下
    2017-07-07
  • C++異常捕捉與處理的深入講解

    C++異常捕捉與處理的深入講解

    這篇文章主要給你大家介紹了關(guān)于C++異常捕捉與處理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • C++的程序流程結(jié)構(gòu)你了解多少

    C++的程序流程結(jié)構(gòu)你了解多少

    這篇文章主要為大家詳細(xì)介紹了C++的程序流程結(jié)構(gòu),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • C++實(shí)現(xiàn)LeetCode(118.楊輝三角)

    C++實(shí)現(xiàn)LeetCode(118.楊輝三角)

    這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(118.楊輝三角),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • C++中訪問字符串的三種方法總結(jié)

    C++中訪問字符串的三種方法總結(jié)

    以下是對(duì)C++中訪問字符串的三種方法進(jìn)行了詳細(xì)的總結(jié)介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助
    2013-10-10
  • C++中名稱空間namespace的使用方法示例

    C++中名稱空間namespace的使用方法示例

    namespace中文意思是命名空間或者叫名字空間,下面這篇文章主要給大家介紹了關(guān)于C++中名稱空間namespace使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起看看吧。
    2017-12-12
  • C/C++ 獲取Windows系統(tǒng)的位數(shù)32位或64位的實(shí)現(xiàn)代碼

    C/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

最新評(píng)論