当前位置:网站首页>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
边栏推荐
- 全志A33使用主线U-Boot
- 数据降维——因子分析
- 新手必看,点击两个按钮切换至不同的内容
- 云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统
- R language ggplot2 visualization: gganimate package creates dynamic histogram animation (GIF) and uses transition_ The States function displays a histogram step by step along a given dimension in the
- How can retail enterprises open the second growth curve under the full link digital transformation
- 电商系统中常见的 9 大坑,你踩过没?
- Exness in-depth good article: dynamic series - Case Analysis of gold liquidity (V)
- PyTorch函数中的__call__和forward函数
- 开发固定资产管理系统,开发固定资产管理系统用什么语音
猜你喜欢

How to copy and paste interlaced in Excel

聊聊电商系统中红包活动设计

juypter notebook 修改默认打开文件夹以及默认浏览器

ICDE 2023|TKDE Poster Session(CFP)

M2dgr: slam data set of multi-source and multi scene ground robot (ICRA 2022)

Novice must see, click two buttons to switch to different content

教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5

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

Looking for innocence in New York -- a beautiful day at the discovery center of Legoland, New Jersey

消息队列消息丢失和消息重复发送的处理策略
随机推荐
STM32G0 USB DFU 升级校验出错-2
线程应用实例
In pytorch function__ call__ And forward functions
Emmet基础语法
Mini Golf Course: a good place for leisure and tourism in London
全链路数字化转型下,零售企业如何打开第二增长曲线
学习八股文的知识点~~1
Preprocessing and preprocessing macros
MySQL advanced learning summary 8: overview of InnoDB data storage structure page, internal structure of page, row format
UML class diagram
Imitation Jingdong magnifying glass effect (pink teacher version)
Hospital online inquiry source code hospital video inquiry source code hospital applet source code
2022编译原理期末考试 回忆版
R language uses the lsnofunction function function of epidisplay package to list all objects in the current space, except user-defined function objects
Excel查找一列中的相同值,删除该行或替换为空值
How to copy and paste interlaced in Excel
SQL training 2
高频面试题
Markdown basic grammar
Yunna | why use the fixed asset management system and how to enable it