当前位置:网站首页>Qpropertyanimation use and toast case list in QT
Qpropertyanimation use and toast case list in QT
2022-07-02 19:12:00 【Crazy excavator】
List of articles
0 introduce
I learned about animation recently , Inadvertently come into contact with the use QPropertyAnimation To complete similar toast The effect of things
The main function :
1、toast The implementation position can be changed according to the parent window ;
2、 The default display time is one minute , Adjustable
1、QPropertyAnimation Built in effect
QPropertyAnimation yes Qt Built in animation class , This class can realize simple control animation effect , For example, the movement of controls 、 The zoom 、 Opacity these to animate ( Before using an effect, you need to use setPropertyName Function to specify the required animation attribute name , The following three are Qt Defined ).
1、 Move (pos): It mainly realizes the mobile effect , Such as moving from one point to another , The type of variable used is QPoint etc. .
2、 The zoom (geometry): Zoom and move effects can be achieved , This property can realize the scaling of the specified control , And it can move on the basis of scaling .
3、 The opacity (windowOpacity): Realize the transparency setting of the control ( However, this attribute can only be used for top-level windows , These are not valid for normal controls ).
2、QPropertyAnimation Custom effects
See citation .
3、QPropertyAnimation toast Realization
1.toast The header file
The code is as follows ( Example ):
#ifndef TOAST_H
#define TOAST_H
#include <QtWidgets/QWidget>
#include <QLabel>
class Toast : public QWidget
{
Q_OBJECT
public:
Toast(QWidget *parent = Q_NULLPTR);
~Toast();
void setText(const QString& text);
void showAnimation(int timeout = 500);
void movetoPosition(int x,int y);
static void showTip(const QString& text,int x,int y,QWidget* parent = nullptr);
private:
QLabel *label = nullptr;
QWidget *widget = nullptr;
};
#endif // TOAST_H
2.toast cpp
The code is as follows ( Example ):
#include "toast.h"
#include <QPropertyAnimation>
#include <QScreen>
#include <QGuiApplication>
#include <QPainter>
#include <QTimer>
#include <QDebug>
#include <QGridLayout>
Toast::Toast(QWidget *parent)
: QWidget(parent)
{
label = new QLabel();
label->setStyleSheet("background-color: rgba(0,0,0,0.80);\nborder-radius: 26px;\ncolor: #FFFFFF;\nfont-family: microsoft yahei;\nfont-size: 16px;\npadding-left:25px;\npadding-right:25px;");
QGridLayout *layout = new QGridLayout;
layout->addWidget(label);
widget = new QWidget();
widget->setWindowFlags(windowFlags() | Qt::FramelessWindowHint | Qt::Tool);
widget->setAttribute(Qt::WA_TranslucentBackground, true);
widget->setGeometry(0,0,170,52);
widget->setLayout(layout);
QScreen* pScreen = QGuiApplication::primaryScreen();
widget->move((pScreen->size().width() - widget->width()) / 2, pScreen->size().height() /2);
widget->setVisible(true);
}
Toast::~Toast()
{
delete label;
delete widget;
}
void Toast::setText(const QString& text)
{
label->setText(text);
}
void Toast::showAnimation(int timeout)
{
QTimer::singleShot(timeout, [&]
{
QPropertyAnimation *animation = new QPropertyAnimation(this, "windowOpacity");
animation->setDuration(500);
animation->setStartValue(1);
animation->setEndValue(0);
animation->start();
connect(animation, &QPropertyAnimation::finished, [&]
{
close();
deleteLater();
});
});
}
void Toast::movetoPosition(int x,int y)
{
widget->move(x,y);
}
void Toast::showTip(const QString& text ,int x,int y,QWidget* parent /*= nullptr*/)
{
Toast* toast = new Toast(parent);
toast->setWindowFlags(toast->windowFlags() | Qt::WindowStaysOnTopHint);
toast->setText(text);
toast->adjustSize();
toast->movetoPosition(x, y);
toast->showAnimation();
}
3.main cpp
void MainWindow::on_pushButton_4_clicked()
{
Toast::showTip(" Test success ! Test success ! Test success ! Test success ! Test success !",this->x(),this->y());
}
void MainWindow::on_pushButton_5_clicked()
{
qDebug()<<"12";
// Toast::showTip(QApplication::translate("Dialog", "Please set destination IP !"));
Toast::showTip(" Test success !",this->x(),this->y());
qDebug()<<"123";
}void MainWindow::on_pushButton_4_clicked()
{
Toast::showTip(" Test success ! Test success ! Test success ! Test success ! Test success !",this->x(),this->y());
}
4、 summary
The article refers to the quoted content , Then implement a toast, Just include the header file and cpp It can be used in the program
5、 quote
1、QT Custom attribute animation
2、QT Realization toast effect
边栏推荐
- Deep learning mathematics foundation
- 潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失
- [100 cases of JVM tuning practice] 01 - introduction of JVM and program counter
- Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
- 页面标题组件
- Progress progress bar
- R language uses the lsnofunction function function of epidisplay package to list all objects in the current space, except user-defined function objects
- MySQL advanced learning summary 7: MySQL data structure - Comparison of hash index, AVL tree, B tree and b+ tree
- Yolov3 trains its own data set to generate train txt
- [fluent] dart data type (VaR data type | object data type)
猜你喜欢
The difference between interceptor and filter
新加坡暑假旅游攻略:一天玩转新加坡圣淘沙岛
Obligatoire pour les débutants, cliquez sur deux boutons pour passer à un contenu différent
According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
[100 cases of JVM tuning practice] 01 - introduction of JVM and program counter
为什么要做企业固定资产管理系统,企业如何加强固定资产管理
Novice must see, click two buttons to switch to different content
医院在线问诊源码 医院视频问诊源码 医院小程序源码
STM32G0 USB DFU 升级校验出错-2
Excel查找一列中的相同值,删除该行或替换为空值
随机推荐
新加坡暑假旅遊攻略:一天玩轉新加坡聖淘沙島
2022 software engineering final exam recall Edition
ORA-01455: converting column overflows integer datatype
The difference between interceptor and filter
Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
SQL training 2
云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统
Hospital online inquiry source code hospital video inquiry source code hospital applet source code
Emmet基础语法
页面标题组件
Date tool class (updated from time to time)
线程应用实例
Transformation of thinking consciousness is the key to the success or failure of digital transformation of construction enterprises
Markdown basic grammar
预处理和预处理宏
Progress-进度条
【ERP软件】ERP体系二次开发有哪些危险?
When converting from list to map, if a certain attribute may cause key duplication and exceptions, you can set the way to deal with this duplication
高频面试题
教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5