当前位置:网站首页>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
边栏推荐
- Date tool class (updated from time to time)
- M2dgr: slam data set of multi-source and multi scene ground robot (ICRA 2022)
- R语言ggplot2可视化分面图(facet):gganimate包基于transition_time函数创建动态散点图动画(gif)
- QT中的QPropertyAnimation使用和toast案列
- The R language dplyr package rowwise function and mutate function calculate the maximum value of multiple data columns in each row in the dataframe data, and generate the data column (row maximum) cor
- Excel查找一列中的相同值,删除该行或替换为空值
- Looking for innocence in New York -- a beautiful day at the discovery center of Legoland, New Jersey
- 潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失
- Crypto usage in nodejs
- R language dplyr package Na_ The if function converts the control in the vector value into the missing value Na, and converts the specified content into the missing value Na according to the mapping r
猜你喜欢
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5
[100 cases of JVM tuning practice] 01 - introduction of JVM and program counter
What is 9D movie like? (+ common sense of dimension space)
【测试开发】软件测试—概念篇
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
How to clean up discarded PVs and their corresponding folders
教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5
How to copy and paste interlaced in Excel
论文导读 | 机器学习在数据库基数估计中的应用
Obligatoire pour les débutants, cliquez sur deux boutons pour passer à un contenu différent
随机推荐
ORA-01455: converting column overflows integer datatype
Hospital online inquiry source code hospital video inquiry source code hospital applet source code
学习八股文的知识点~~1
C的内存管理
消息队列消息丢失和消息重复发送的处理策略
Mysql高级篇学习总结7:Mysql数据结构-Hash索引、AVL树、B树、B+树的对比
使用xml文件打印mybaties-log插件的方式
MySQL advanced learning summary 8: overview of InnoDB data storage structure page, internal structure of page, row format
Troubleshooting: kubectl reports an error validationerror: unknown field \u00a0
电商系统中常见的 9 大坑,你踩过没?
教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5
使用CLion编译OGLPG-9th-Edition源码
高频面试题
数据降维——主成分分析
Learning summary of MySQL advanced 6: concept and understanding of index, detailed explanation of b+ tree generation process, comparison between MyISAM and InnoDB
Page title component
【测试开发】软件测试—概念篇
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
R语言ggplot2可视化:gganimate包创建动态柱状图动画(gif)、使用transition_states函数在动画中沿给定维度逐步显示柱状图
UML class diagram