当前位置:网站首页>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 !!!");
边栏推荐
- Add the resources directory under test in idea
- 弹性布局(display:flex;)属性详解
- Modal and modeless dialogs for QT
- Is it safe to open a stock account online?
- Js- get the mouse coordinates and follow them
- semget No space left on device
- Arithmetic operations and expressions
- Position (5 ways)
- Stderr and stdout related standard outputs and other C system APIs
- QT inline dialog
猜你喜欢

15 -- 最接近原点的 K 个点

SPARQL learning notes of query, an rrdf query language

Learning notes on February 8, 2022 (C language)

Learning C language today is the first time to learn C language. In college, C linguistics is not good, but I want to make progress, so I found a beep video on the Internet to learn C language
![[Ocean University of China] information sharing for the first and second examinations of postgraduate entrance examination](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[Ocean University of China] information sharing for the first and second examinations of postgraduate entrance examination

Learning notes on February 5, 2022 (C language)

定位position(5种方式)

Single user mode

One question per day,

Gif动图如何裁剪?收下这个图片在线裁剪工具
随机推荐
Learning notes on February 8, 2022 (C language)
QT database connection deletion
Usage of qlist
【Try to Hack】vulnhub DC1
Iterator failure condition
3. Sequential structure multiple choice questions
弹性布局(display:flex;)属性详解
Go closure usage example
‘make_ unique’ is not a member of ‘std’
[Ocean University of China] Data Sharing for retest of initial Examination
Use Matplotlib to draw a line chart
Boost listening port server
[deep learning] multi task learning of multiple datasets data sets missing labels
QQ情话糖果情话内容获取并保存
How to package rpm
Dynamic memory allocation
JS select all exercise
dmsetup命令
About?: Notes for
Differences between member variables and local variables