当前位置:网站首页>QT pattern prompt box implementation
QT pattern prompt box implementation
2022-06-25 15:07:00 【Knowledge first】
effect
For example, it is used when the software is closed , Realize the software dodge function .


Realize the idea
It's very simple , Is to inherit QFrame class , Then put two... In the interface QLabel object . One lab Used to display the background image , A text used to display the prompt .
Background picture a picture of Baidu , Then subtract the background , As shown in the figure below :
Showing the text QLabel The position to be corrected is in the frame , Then the display is centered 、 Auto wrap, etc .
Because there is no manual shutdown function , So it also needs to be closed automatically and regularly , In fact, the implementation is the same as that in a pop-up article I wrote before .
Pop up window :https://blog.csdn.net/weixin_42887343/article/details/109303972
Implementation code
The header file
#ifndef LIGHTMESSAGE_H
#define LIGHTMESSAGE_H
#include <QFrame>
class LightMessage : public QFrame
{
Q_OBJECT
public:
static void information(QWidget *parent,
QString info,
int time = 3000,
QString strFontSize = "20px",
QString strColor = "#000000");
signals:
private:
explicit LightMessage(QWidget *parent = nullptr,
QString info = "",
QString strFontSize = "20px",
QString strColor = "#000000");
};
#endif // LIGHTMESSAGE_H
cpp file
#include "lightMessage.h"
#include <QLabel>
#include <QTimer>
#include <QPropertyAnimation>
#include <QDesktopWidget>
LightMessage::LightMessage(QWidget *parent,QString info, QString strFontSize, QString strColor) : QFrame(parent)
{
// Window settings
//int width = 200;
//int height = 134;
this->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
this->setAttribute(Qt::WA_TranslucentBackground); // Set the form background transparent
this->resize(200,134);
// Brush the background picture
QLabel *pLabelImg = new QLabel(this);
pLabelImg->resize(width(),height());
pLabelImg->setGeometry(0,0,width(),height());
QImage img(":/new/prefix1/resource/lightMessage.png");
QImage img2 = img.scaled(width(),height(),Qt::KeepAspectRatio);
pLabelImg->setPixmap(QPixmap::fromImage(img2));
// Brush text
QLabel *pLabelStr = new QLabel(this);
pLabelStr->setAlignment(Qt::AlignCenter);
pLabelStr->setStyleSheet(QString("QLabel{color:%1;font:%2 SimHei;border-radius:5px;}")
.arg(strColor).arg(strFontSize));
pLabelStr->setText(info);
pLabelStr->adjustSize();
pLabelStr->setGeometry(width()/25,height()/13.5,width()/1.45,height()/2.4);
}
void LightMessage::information(QWidget *parent,QString info, int time, QString strFontSize, QString strColor)
{
LightMessage *widgetPtr = new LightMessage(parent,info,strFontSize,strColor);
if(parent)
{
int x = parent->pos().x();
int y = parent->pos().y() - widgetPtr->height();
widgetPtr->move(x,y-2);
}
if(time < 1100) time = 1100;
QTimer::singleShot(time-1000,widgetPtr,[=]()
{
QPropertyAnimation *pAnimation = new QPropertyAnimation(widgetPtr,"windowOpacity",widgetPtr);
pAnimation->setDuration(1000);
pAnimation->setEasingCurve(QEasingCurve::InCirc);
pAnimation->setStartValue(1.0);
pAnimation->setEndValue(0.0);
pAnimation->start();
connect(pAnimation,&QPropertyAnimation::finished,[=]{
delete widgetPtr;
});
});
widgetPtr->show();
}
Note that :/new/prefix1/resource/lightMessage.png It is the picture of the front buckle , Then add the image path of the project resource .
Usage method
The method of use is very simple , as follows :
LightMessage::information(nullptr," o ... Thank you !",2000,"16px");
LightMessage::information(this,"... I'll dance again !!!");
边栏推荐
- Is it safe to open an online stock account? Who knows
- Generation method and usage of coredump
- Is it safe to open a stock account online?
- 多张动图怎样合成一张gif?仅需三步快速生成gif动画图片
- Remove interval (greedy)
- 55 specific ways to improve program design (2)
- Cross compilation correlation of curl Library
- QQ情话糖果情话内容获取并保存
- Brain tree (I)
- 从0到1完全掌握 XSS
猜你喜欢

1090.Phone List

Custom structure type

Source code analysis of zeromq lockless queue

basic_ String mind map

SPARQL learning notes of query, an rrdf query language

Solution of push code failure in idea

Sequential programming 1

Flexible layout (display:flex;) Attribute details

How to crop GIF dynamic graph? Take this picture online clipping tool

Build a minimalist gb28181 gatekeeper and gateway server, establish AI reasoning and 3D service scenarios, and then open source code (I)
随机推荐
Daily question, Caesar code,
Single user mode
Time stamp calculation and audio-visual synchronization of TS stream combined video by ffmpeg protocol concat
C language escape character and its meaning
[try to hack] vulhub shooting range construction
QQ情话糖果情话内容获取并保存
Stack and queue
Differences between member variables and local variables
Cross compilation correlation of curl Library
Brain tree (I)
Common dynamic memory errors
网上办理股票开户安全吗?
Learning notes on February 8, 2022 (C language)
Semaphore function
Js- get the mouse coordinates and follow them
Position (5 ways)
Automatic correlation between QT signal and slot
Breakpad usage and DMP analysis
Daily question, magic square simulation
Disable scrolling in the iPhone web app- Disable scrolling in an iPhone web application?