当前位置:网站首页>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 !!!");
边栏推荐
- ‘make_ unique’ is not a member of ‘std’
- Character encoding minutes
- Design and implementation of thread pool
- Usage of pure virtual functions
- 2. operator and expression multiple choice questions
- Flexible layout (display:flex;) Attribute details
- Is it normal to dig for money? Is it safe to open a stock account?
- Solution of push code failure in idea
- BM setup process
- Using Visual Studio
猜你喜欢
Jaspersoft studio adding MySQL database configuration
What moment makes you think there is a bug in the world?
Using Sphinx to automatically generate API documents from py source files
Sequential programming 1
[Ocean University of China] information sharing for the first and second examinations of postgraduate entrance examination
Yolov3 spp Darknet version to caffemodel and then to OM model
Source code analysis of zeromq lockless queue
电源自动测试系统NSAT-8000,精准高速可靠的电源测试设备
About the problem of kicad stuck in win10 version, version 6 x
【Try to Hack】vulnhub DC1
随机推荐
Dmsetup command
New title of PTA
Iterator failure condition
How to deal with mining process
Usage of qlist
55 specific ways to improve program design (2)
@Font face fonts only work on their own domain - @font-face fonts only work on their own domain
Jaspersoft studio installation
Fishing detection software
QT file reading -qfile
Design and implementation of thread pool
Build a minimalist gb28181 gatekeeper and gateway server, establish AI reasoning and 3D service scenarios, and then open source code (I)
JS select all exercise
挖财是正规的吗?股票开户安全吗?
15 -- 最接近原点的 K 个点
Open a restaurant
dmsetup命令
In 2022, the score line of Guangdong college entrance examination was released, and several families were happy and several worried
Common operations in VIM
Use Matplotlib to draw a line chart