最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)文章

  • 利用Matlab復(fù)刻舉牌加油小人生成器

    利用Matlab復(fù)刻舉牌加油小人生成器

    upuptoyou是一款非常有創(chuàng)意的小工具,可以在線生成舉牌小人,看起來很可愛,也比較有趣,并能用于表白,或節(jié)日送祝福等場(chǎng)景。本文將用Matlab復(fù)刻這一小工具,需要的可以參考一下
    2022-03-03
  • 你知道C語言函數(shù)調(diào)用常用的2種方式嗎

    你知道C語言函數(shù)調(diào)用常用的2種方式嗎

    本篇博客會(huì)講解C語言函數(shù)調(diào)用的2種方式,分別是:傳值調(diào)用和傳址調(diào)用。這2種函數(shù)調(diào)用方式有什么區(qū)別呢?為什么會(huì)有不同的效果呢?分別有哪些用途呢?下面就來一一展開
    2023-04-04
  • 淺析VSCode launch.json中的各種替換變量的意思 ${workspaceFolder} ${file} ${fileBasename} ${fileDirname}等

    淺析VSCode launch.json中的各種替換變量的意思 ${workspaceFolder} ${file} $

    這篇文章主要介紹了VSCode launch.json中的各種替換變量的意思 ${workspaceFolder} ${file} ${fileBasename} ${fileDirname}等,非常不錯(cuò)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • C++分步實(shí)現(xiàn)職工管理系統(tǒng)詳解

    C++分步實(shí)現(xiàn)職工管理系統(tǒng)詳解

    這篇文章主要為大家詳細(xì)介紹了基于C++實(shí)現(xiàn)職工管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-10-10
  • C++中求組合數(shù)的各種方法總結(jié)詳解

    C++中求組合數(shù)的各種方法總結(jié)詳解

    本篇文章是對(duì)C++中的求組合數(shù)的各種方法進(jìn)行了詳細(xì)的介紹。需要的朋友參考下
    2013-05-05
  • C++實(shí)現(xiàn)自底向上的歸并排序算法

    C++實(shí)現(xiàn)自底向上的歸并排序算法

    這篇文章主要介紹了C++實(shí)現(xiàn)自底向上的歸并排序算法,結(jié)合實(shí)例形式較為詳細(xì)的分析總結(jié)了自底向上的歸并排序算法的原理與具體實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2015-12-12
  • VisualStudio2022不支持.NET Framework 4.0項(xiàng)目解決辦法

    VisualStudio2022不支持.NET Framework 4.0項(xiàng)目解決辦法

    本文主要介紹了VisualStudio2022不支持.NET Framework 4.0項(xiàng)目解決辦法,文中通過圖文的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-09-09
  • C語言實(shí)現(xiàn)密碼強(qiáng)度檢測(cè)

    C語言實(shí)現(xiàn)密碼強(qiáng)度檢測(cè)

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)密碼強(qiáng)度檢測(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • c++ 數(shù)組定義及初始化詳解

    c++ 數(shù)組定義及初始化詳解

    這篇文章主要介紹了c++ 數(shù)組定義及初始化詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • C/C++實(shí)現(xiàn)圖形學(xué)掃描線填充算法

    C/C++實(shí)現(xiàn)圖形學(xué)掃描線填充算法

    這篇文章主要介紹了C/C++實(shí)現(xiàn)圖形學(xué)掃描線填充算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04

最新評(píng)論

佛坪县| 吉水县| 长葛市| 酉阳| 左云县| 威海市| 潮安县| 建始县| 高要市| 安远县| 武城县| 武邑县| 洛隆县| 莒南县| 泗洪县| 炉霍县| 沛县| 衡南县| 绍兴市| 盐亭县| 南阳市| 延长县| 武夷山市| 合江县| 盱眙县| 雅安市| 晴隆县| 台江县| 大理市| 抚宁县| 辽宁省| 邵武市| 芒康县| 平远县| 三原县| 习水县| 临洮县| 六盘水市| 华坪县| 淮滨县| 平谷区|