当前位置:网站首页>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
边栏推荐
- How to clean up discarded PVs and their corresponding folders
- ORA-01455: converting column overflows integer datatype
- 教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5
- [test development] takes you to know what software testing is
- 迷你高尔夫球场:伦敦休闲旅游好去处
- PHP非对称加密方法私钥及公钥加密解密的方法
- 2022.7.1-----leetcode.241
- 使用CLion编译OGLPG-9th-Edition源码
- 9D电影是怎样的?(+维度空间常识)
- Golang concurrent programming goroutine, channel, sync
猜你喜欢

PyTorch函数中的__call__和forward函数

论文导读 | 机器学习在数据库基数估计中的应用

Codeworks 5 questions per day (1700 average) - day 4

【测试开发】软件测试—概念篇

How to play when you travel to Bangkok for the first time? Please keep this money saving strategy

Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径

How to enable the run dashboard function of idea

Excel查找一列中的相同值,删除该行或替换为空值

MySQL advanced learning summary 7: MySQL data structure - Comparison of hash index, AVL tree, B tree and b+ tree
![[100 cases of JVM tuning practice] 02 - five cases of virtual machine stack and local method stack tuning](/img/59/6c776e0607a52962b72fbea2e64c8e.png)
[100 cases of JVM tuning practice] 02 - five cases of virtual machine stack and local method stack tuning
随机推荐
思维意识转变是施工企业数字化转型成败的关键
性能测试如何创造业务价值
Codeworks 5 questions per day (1700 average) - day 4
新手必看,点击两个按钮切换至不同的内容
R language ggplot2 visualization: visualize the line chart and add customized X-axis label information to the line chart using labs function
Golang并发编程——goroutine、channel、sync
【测试开发】软件测试—概念篇
MySQL advanced learning summary 7: MySQL data structure - Comparison of hash index, AVL tree, B tree and b+ tree
在纽约寻找童真——新泽西州乐高乐园探索中心的美好一天
[0701] [paper reading] allowing data imbalance issue with perforated input during influence
线程应用实例
What is 9D movie like? (+ common sense of dimension space)
Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5
MySQL advanced learning summary 8: overview of InnoDB data storage structure page, internal structure of page, row format
从list转化成map的时候,如果根据某一属性可能会导致key重复而异常,可以设置处理这种重复的方式
C文件输入操作
[daily question] the next day
R language dplyr package filter function filters dataframe data. If the name of the data column (variable) to be filtered contains quotation marks, you need to use!! SYM syntax processing, otherwise n
[100 cases of JVM tuning practice] 03 -- four cases of JVM heap tuning
R语言dplyr包na_if函数把向量数值中的控制转化为缺失值NA、按照映射规则把指定内容转化为缺失值NA