当前位置:网站首页>QT中的QPropertyAnimation使用和toast案列
QT中的QPropertyAnimation使用和toast案列
2022-07-02 17:45:00 【疯狂的挖掘机】
文章目录
0 引入
最近使用学习到动画这一块,无意中接触到使用QPropertyAnimation来完成 类似 toast效果的东西

主要功能:
1、toast实现位置可以根据父窗口而改变;
2、显示时间默认是一分钟,可调
1、QPropertyAnimation自带效果
QPropertyAnimation是Qt自带的动画类,该类可以实现简单的控件动画效果,比如对控件的移动、缩放、不透明度这些来做动画效果(使用某效果之前需要使用setPropertyName函数指定需要的动画属性名,以下三个是Qt已经定义好的)。
1、移动(pos):主要实现移动效果,如从某个点移动到另一个点,所使用的变量类型为QPoint等。
2、缩放(geometry):可实现缩放和移动效果,该属性可以实现指定控件的缩放,并且在缩放的基础上还能实现移动。
3、不透明度(windowOpacity):实现控件的透明度设置(不过该属性只能对顶级窗口使用,对普通控件这些无效)。
2、QPropertyAnimation自定义效果
见引用。
3、QPropertyAnimation toast实现
1.toast头文件
代码如下(示例):
#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
代码如下(示例):
#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("测试成功!测试成功!测试成功!测试成功!测试成功!",this->x(),this->y());
}
void MainWindow::on_pushButton_5_clicked()
{
qDebug()<<"12";
// Toast::showTip(QApplication::translate("Dialog", "Please set destination IP !"));
Toast::showTip("测试成功!",this->x(),this->y());
qDebug()<<"123";
}void MainWindow::on_pushButton_4_clicked()
{
Toast::showTip("测试成功!测试成功!测试成功!测试成功!测试成功!",this->x(),this->y());
}
4、总结
文章参考引用的内容,然后自己实现一个toast,只需要包含头文件和cpp就可以在程序中使用了
5、引用
1、QT自定义属性动画
2、QT实现toast效果
边栏推荐
- 高频面试题
- 昨天阿里学长写了一个责任链模式,竟然出现了无数个bug
- 拦截器与过滤器的区别
- R语言使用epiDisplay包的cox.display函数获取cox回归模型汇总统计信息(风险率HR、调整风险率及其置信区间、模型系数的t检验的p值、Wald检验的p值和似然比检验的p值)、汇总统计
- Which securities company has a low, safe and reliable online account opening commission
- @Component cannot get Dao layer
- MySQL advanced learning summary 7: MySQL data structure - Comparison of hash index, AVL tree, B tree and b+ tree
- 新手必看,點擊兩個按鈕切換至不同的內容
- Leetcode(154)——寻找旋转排序数组中的最小值 II
- 【JVM调优实战100例】02——虚拟机栈与本地方法栈调优五例
猜你喜欢
![[daily question] first day](/img/8c/f25cddb6ca86d44538c976fae13c6e.png)
[daily question] first day

After 22 years in office, the father of PowerShell will leave Microsoft: he was demoted by Microsoft for developing PowerShell

开源物联网平台ThingsBoard的安装

A simple PHP personal card issuing program v4.0

Tips for material UV masking

谷歌官方回应:我们没有放弃TensorFlow,未来与JAX并肩发展

LightGroupButton* sender = static_cast<LightGroupButton*>(QObject::sender());

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

STM32G0 USB DFU 升级校验出错-2

思维意识转变是施工企业数字化转型成败的关键
随机推荐
Mini Golf Course: a good place for leisure and tourism in London
SLC、MLC、TLC 和 QLC NAND SSD 之间的区别:哪个更好?
SLAM|如何时间戳对齐?
Hongmeng's fourth learning
9D电影是怎样的?(+维度空间常识)
SQL training 2
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
Mysql高级篇学习总结6:索引的概念及理解、B+树产生过程详解、MyISAM与InnoDB的对比
[Yugong series] July 2022 go teaching course 001 introduction to go language premise
A simple PHP personal card issuing program v4.0
Thoroughly understand the point cloud processing tutorial based on open3d!
How to play when you travel to Bangkok for the first time? Please keep this money saving strategy
What is cloud primordial? This time, I can finally understand!
ICDE 2023|TKDE Poster Session(CFP)
R语言dplyr包rowwise函数、mutate函数计算dataframe数据中多个数据列在每行的最大值、并生成行最大值对应的数据列(row maximum)
The difference between interceptor and filter
MySQL advanced learning summary 7: MySQL data structure - Comparison of hash index, AVL tree, B tree and b+ tree
The student Tiktok publicized that his alma mater was roast about "reducing the seal of enrollment". Netizen: hahahahahahahaha
options should NOT have additional properties
文字编辑器 希望有错误的句子用红色标红,文字编辑器用了markdown