qt 提示框 隔几秒后消失
工作中用到了一个功能:触发显示了提示框,然后几秒后自动消失。
引用:https://blog.csdn.net/a_1_1_1_2/article/details/119949348
将窗口显示时间定时器设置为单次触发;
该提示窗口关闭前,要先停止渐变定时器。
使用方法:
MessageTips *mMessageTips = new MessageTips(tr("screenshot is ok"), this);//截图成功提示
mMessageTips->move(parentWidget()->x() + 470, parentWidget()->y() + 50);
mMessageTips->show();
头文件
1 #ifndef MESSAGETIPS_H 2 #define MESSAGETIPS_H 3 4 #include <QWidget> 5 # pragma execution_character_set("utf-8") 6 7 class QHBoxLayout; 8 class QLabel; 9 class MessageTips : public QWidget 10 { 11 Q_OBJECT 12 public: 13 explicit MessageTips(QString showStr = "默认显示", QWidget *parent = nullptr); 14 15 double getOpacityValue() const; 16 void setOpacityValue(double value); 17 18 int getTextSize() const; 19 void setTextSize(int value); 20 21 QColor getTextColor() const; 22 void setTextColor(const QColor &value); 23 24 QColor getBackgroundColor() const; 25 void setBackgroundColor(const QColor &value); 26 27 QColor getFrameColor() const; 28 void setFrameColor(const QColor &value); 29 30 int getFrameSize() const; 31 void setFrameSize(int value); 32 33 int getShowTime() const; 34 void setShowTime(int value); 35 36 void setCloseTimeSpeed(int closeTime = 100, double closeSpeed = 0.1); 37 38 protected: 39 void paintEvent(QPaintEvent *event) override; 40 41 private: 42 void InitLayout();//初始化窗体的布局和部件 43 QHBoxLayout *hBoxlayout;//布局显示控件布局 44 QLabel *mText;//用于显示文字的控件 45 QString showStr;//显示的字符串 46 47 double opacityValue;//窗体初始化透明度 48 49 QFont *font; 50 int textSize;//显示字体大小 51 QColor textColor;//字体颜色 52 53 QColor backgroundColor;//窗体的背景色 54 QColor frameColor;//边框颜色 55 int frameSize;//边框粗细大小 56 57 int showTime;//显示时间 58 59 int closeTime;//关闭需要时间 60 double closeSpeed;//窗体消失的平滑度,大小0~1 61 62 63 signals: 64 65 }; 66 67 #endif // MESSAGETIPS_H
源文件:
1 #include "messagetips.h" 2 3 #include <QDesktopWidget> 4 #include <QHBoxLayout> 5 #include <QLabel> 6 #include <QPainter> 7 #include <QTimer> 8 #include<QApplication> 9 10 MessageTips::MessageTips(QString showStr, QWidget *parent) : QWidget(parent), 11 opacityValue(1.0), 12 textSize(18), 13 textColor(QColor(255, 255, 255)), 14 backgroundColor(QColor(0, 85, 255, 60)), 15 frameColor(QColor(211, 211, 211, 60)), 16 frameSize(2), 17 showTime(2500), 18 closeTime(100), 19 closeSpeed(0.1), 20 hBoxlayout(new QHBoxLayout(this)), 21 mText(new QLabel(this)) 22 { 23 setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Tool | Qt::X11BypassWindowManagerHint); 24 this->setAttribute(Qt::WA_TranslucentBackground); // ****这里很重要**** 25 this->setAttribute(Qt::WA_TransparentForMouseEvents, true);// 禁止鼠标事件 26 this->showStr = showStr; 27 hBoxlayout->addWidget(mText); 28 InitLayout(); 29 } 30 31 void MessageTips::InitLayout() 32 { 33 this->setWindowOpacity(opacityValue); 34 35 //文字显示居中,设置字体,大小,颜色 36 font = new QFont("微软雅黑", textSize, QFont::Bold); 37 mText->setFont(*font); 38 mText->setText(showStr); 39 mText->setAlignment(Qt::AlignCenter); 40 QPalette label_pe;//设置字体颜色 41 label_pe.setColor(QPalette::WindowText, textColor); 42 mText->setPalette(label_pe); 43 44 QTimer *mtimer = new QTimer(this);//隐藏的定时器 45 mtimer->setTimerType(Qt::PreciseTimer); 46 connect(mtimer, &QTimer::timeout, this, [=]() { 47 if (opacityValue <= 0) 48 { 49 mtimer->stop();//关闭定时器 50 this->close(); 51 } 52 opacityValue = opacityValue - closeSpeed; 53 this->setWindowOpacity(opacityValue); //设置窗口透明度 54 }); 55 56 57 QTimer *mShowtimer = new QTimer(this);//显示时间的定时器 58 mShowtimer->setSingleShot(true); 59 mShowtimer->setTimerType(Qt::PreciseTimer);// 修改定时器对象的精度 60 connect(mShowtimer, &QTimer::timeout, this, [=]() { 61 mtimer->start(closeTime);//执行延时自动关闭 62 }); 63 mShowtimer->start(showTime); 64 65 //设置屏幕居中显示 66 QDesktopWidget* desktop = QApplication::desktop(); // =qApp->desktop();也可以 67 this->move((desktop->width() - this->width()) / 2, (desktop->height() - this->height()) / 2); 68 this->setAttribute(Qt::WA_TransparentForMouseEvents, true);// 禁止鼠标事件 69 } 70 71 void MessageTips::paintEvent(QPaintEvent *event) 72 { 73 QPainter painter(this); 74 painter.setBrush(QBrush(backgroundColor));//窗体的背景色 75 76 painter.setPen(QPen(frameColor, frameSize));//窗体边框的颜色和笔画大小 77 QRectF rect(0, 0, this->width(), this->height()); 78 painter.drawRoundedRect(rect, 15, 15); // round rect 79 } 80 81 82 83 84 //设置关闭的时间和速度,speed大小限定0~1 85 void MessageTips::setCloseTimeSpeed(int closeTime, double closeSpeed) 86 { 87 if (closeSpeed>0 && closeSpeed <= 1) { 88 this->closeSpeed = closeSpeed; 89 } 90 this->closeTime = closeTime; 91 InitLayout(); 92 } 93 94 95 int MessageTips::getShowTime() const 96 { 97 return showTime; 98 } 99 100 void MessageTips::setShowTime(int value) 101 { 102 showTime = value; 103 InitLayout(); 104 } 105 106 int MessageTips::getFrameSize() const 107 { 108 return frameSize; 109 } 110 111 void MessageTips::setFrameSize(int value) 112 { 113 frameSize = value; 114 } 115 116 QColor MessageTips::getFrameColor() const 117 { 118 return frameColor; 119 } 120 121 void MessageTips::setFrameColor(const QColor &value) 122 { 123 frameColor = value; 124 } 125 126 QColor MessageTips::getBackgroundColor() const 127 { 128 return backgroundColor; 129 } 130 131 void MessageTips::setBackgroundColor(const QColor &value) 132 { 133 backgroundColor = value; 134 } 135 136 QColor MessageTips::getTextColor() const 137 { 138 return textColor; 139 } 140 141 void MessageTips::setTextColor(const QColor &value) 142 { 143 textColor = value; 144 InitLayout(); 145 } 146 147 int MessageTips::getTextSize() const 148 { 149 return textSize; 150 } 151 152 void MessageTips::setTextSize(int value) 153 { 154 textSize = value; 155 InitLayout(); 156 } 157 158 double MessageTips::getOpacityValue() const 159 { 160 return opacityValue; 161 } 162 163 void MessageTips::setOpacityValue(double value) 164 { 165 opacityValue = value; 166 InitLayout(); 167 }