In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "QT how to achieve timing off message box", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "QT how to achieve timing off message box" this article.
I. brief introduction
Using Qt to simply implement the prompt box can be closed automatically at regular times.
Second, effect
III. Engineering structure
UI interface
IV. Source files
NoticeWidget.pro file
QT + = core gui greaterThan (QT_MAJOR_VERSION, 4): QT + = widgets TARGET = NoticeTEMPLATE = app SOURCES + = main.cpp\ mainwindow.cpp\ noticewidget.cpp HEADERS + = mainwindow.h\ noticewidget.h FORMS + = mainwindow.ui
Mainwindow.h file
# ifndef MAINWINDOW_H#define MAINWINDOW_H# include 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 file
# include "mainwindow.h" # include "ui_mainwindow.h" # include "noticewidget.h" MainWindow::MainWindow (QWidget * parent): QMainWindow (parent), ui (new Ui::MainWindow) {ui- > setupUi (this); setWindowTitle ("scheduled automatic closing message box"); ui- > plainTextEditMsg- > setPlainText ("scheduled automatic closing message box test, simple test example");} MainWindow::~MainWindow () {delete ui } void MainWindow::on_pushButtonShowNotice_clicked () {static NoticeWidget noticeWin; noticeWin.Notice (this, ui- > plainTextEditMsg- > toPlainText (), 3000);}
Noticewidget.h file
# ifndef _ NoticeWidget_H_#define _ NoticeWidget_H_# include # include / / timer interval, unit ms#define TIMER_INTERVAL_MS 50 / / default prompt time 1s#define NOTICE_DEF_DELAY_CNT (1000/TIMER_INTERVAL_MS) / / maximum transparency 255 That is, opacity # define TRANSPARENT_MAX_VAL 255 / / Transparency diminishing value # define TRANSPARENT_CUT_VAL (TRANSPARENT_MAX_VAL/NOTICE_DEF_DELAY_CNT + 1) / / size ratio # define SIZE_SCALE 0.8 / / spacing adjustment # define PADDING 4 / / style, font color: White Fillet Background color transparency # 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; / / width of one row int mBaseHeight; / / height of one row int mTransparentVal;// transparency 0cm 255, the smaller the value, the more transparent}; # endif / / _ NoticeWidget_H_
Noticewidget.cpp file
# include "noticewidget.h" NoticeWidget::NoticeWidget (QWidget * parent): mParentPtr (parent), mTimerPtr (nullptr), mTimerCount (NOTICE_DEF_DELAY_CNT), mBaseWidth (0), mBaseHeight (0), mTransparentVal (TRANSPARENT_MAX_VAL) {/ / text centered setAlignment (Qt::AlignCenter); / / timer, 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) {/ / parent window position change ChangeSize (); move (pt);}} / / the last second began to fade away if (mTimerCount mTransparentVal) {mTransparentVal = 0 } / / Control transparency setStyleSheet (QString (STYLE_SHEET) .arg (mTransparentVal));}} else {/ / display end mTimerPtr- > stop (); setVisible (false);} / / set the message void NoticeWidget::SetMesseage (const QString & msg, int delay_ms) {mParentPtr = parentWidget (); QFontMetrics fontMetrics (font ()) MBaseWidth = fontMetrics.width (msg); mBaseHeight = fontMetrics.height () + PADDING; / / set width and height ChangeSize (); / wrap setWordWrap (true); / / set display content setText (msg); / / Center if (nullptr! = mParentPtr) {move ((mParentPtr- > width ()-width ()) > 1, (mParentPtr- > height ()-height ()) > > 1) } setVisible (true); / / display setStyleSheet (QString (STYLE_SHEET) .arg (TRANSPARENT_MAX_VAL)); / / set the style, opaque mTimerCount = delay_ms/TIMER_INTERVAL_MS + 1 scramble / delay count calculation mTransparentVal = TRANSPARENT_MAX_VAL;} / / follow the parent window size change void NoticeWidget::ChangeSize () {if (nullptr! = mParentPtr) {double wd = mParentPtr- > width () * SIZE_SCALE / / 80% of the width of the parent window is setFixedSize ((int) wd, mBaseHeight* (mBaseWidth/wd + 1));}} / / displays messages. You can immediately turn off the display of void NoticeWidget::Notice (QWidget * parent, const QString & msg, const int delay_ms) {if (mTimerPtr- > isActive ()) {mTimerPtr- > stop (); setVisible (false) by setting delay_ms=0. } / / if the message is empty, return if (msg.isEmpty () | | 0 > = delay_ms) {return;} setParent (parent); SetMesseage (msg, delay_ms); mTimerPtr- > start (TIMER_INTERVAL_MS); / / start counting}
Main.cpp file
# include "mainwindow.h" # include int main (int argc, char * argv []) {QApplication a (argc, argv); MainWindow w; w.show (); return a.exec ();} above is all the content of the article "how to turn off the message box on a regular basis in QT". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.