1 封装自定义按钮 myPushBttton

2 构造函数 (默认图片,按下后显示图片)

3 测试开始按钮

4 开始制作特效

5 zoom1 向下弹跳

6 zoom2 向上弹跳

代码如下

main.h

#ifndef MAINMAIN_H
#define MAINMAIN_H #include <QMainWindow>
#include<QEvent> QT_BEGIN_NAMESPACE
namespace Ui { class MainMain; }
QT_END_NAMESPACE class MainMain : public QMainWindow
{
Q_OBJECT public:
MainMain(QWidget *parent = nullptr);
~MainMain();
protected:
void paintEvent(QPaintEvent *event); private:
Ui::MainMain *ui;
};
#endif // MAINMAIN_H

main.cpp

#include "mainmain.h"
#include "ui_mainmain.h"
#include<QIcon>
#include<QAction>
#include<QPainter>
#include"mypushbutton.h"
#include<QDebug> MainMain::MainMain(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainMain)
{
ui->setupUi(this); //设置场景大小
setFixedSize(320,588);
//设置标题
setWindowTitle("my text game");
//设置图标
setWindowIcon(QIcon(":/res/szan.png")); //退出
connect(ui->actionquit2,&QAction::triggered,[=](){
this->close();
}); myPushButton *myBtn = new myPushButton(":/res/szan.png");
myBtn->setParent(this);
myBtn->move(this->width()/2-myBtn->width()/2, this->height()*0.7);
connect(myBtn,&myPushButton::clicked,[=](){
qDebug()<<"mybtn clicked!!!";
myBtn->zoom1();
myBtn->zoom2();
}); } MainMain::~MainMain()
{
delete ui;
} //画家事件
void MainMain::paintEvent(QPaintEvent *event)
{
//定义一个画家
QPainter qpainter(this);
//定义一个pixmap
QPixmap pixmap;
//加载图片
pixmap.load(":/res/pjbj.png");
//把图片画上去 图片和屏幕一样大小
qpainter.drawPixmap(0,0,this->width(),this->height(),pixmap); //加载图标还是用pixmap
pixmap.load(":/res/szan.png");
pixmap = pixmap.scaled(pixmap.width()*0.5,pixmap.height()*0.5);
qpainter.drawPixmap(0,0,pixmap); }

自定义的 myPushButton.h

#ifndef MYPUSHBUTTON_H
#define MYPUSHBUTTON_H #include <QWidget>
#include<QPushButton> class myPushButton : public QPushButton
{
Q_OBJECT
public:
explicit myPushButton(QPushButton *parent = nullptr);
myPushButton(QString normalImg,QString pressImg=nullptr);
void zoom1();//向上跳
void zoom2();//向下跳 private:
QString normalImgPath;
QString pressImgPath; signals: }; #endif // MYPUSHBUTTON_H

自定义的 myPushButton.cpp

#include "mypushbutton.h"
#include<QDebug>
#include<QPropertyAnimation>
myPushButton::myPushButton(QPushButton *parent) : QPushButton(parent)
{
} myPushButton::myPushButton(QString normalImg,QString pressImg)
{
this->normalImgPath = normalImg;
this->pressImgPath = pressImg; QPixmap fix;
bool ret = fix.load(normalImg);
if(!ret){
qDebug()<<"图片加载失败!!!";
}
//设置图片固定大小
this->setFixedSize(fix.width(),fix.height());
// 设置不规则图片样式
this->setStyleSheet("QPushButton{border:Opx;}");
//设置图标
this->setIcon(fix);
//设置图标大小
this->setIconSize(QSize(fix.width(),fix.height() ));
}
//向上跳
void myPushButton::zoom1(){
//创建动态对象
QPropertyAnimation *animalton = new QPropertyAnimation(this,"geometry");
//设置间隔时间
animalton->setDuration(200);
//设置起始位置
animalton->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
//设置结束位置
animalton->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
//设置动画曲线
animalton->setEasingCurve(QEasingCurve::OutBounce);
//动起来
animalton->start(); }
//向下跳
void myPushButton::zoom2(){
//创建动态对象
QPropertyAnimation *animalton = new QPropertyAnimation(this,"geometry");
//设置间隔时间
animalton->setDuration(200);
//设置起始位置
animalton->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
//设置结束位置
animalton->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));
//设置动画曲线+10
animalton->setEasingCurve(QEasingCurve::OutBounce);
//动起来
animalton->start();
}

Qt 创建按钮动画的更多相关文章

  1. Unity 2D骨骼动画2:创建真实动画

    http://bbs.9ria.com/thread-401781-1-1.html 在这个系列,我们将关注Unity引擎提供的基于骨骼动画工具.它的主要思想是为了把它应用到你自己的游戏来介绍和教基本 ...

  2. 16款创建CSS3动画的jQuery插件

    jQuery插件是用来扩展jQuery原型对象的方法. 本文搜集了用来为你的站点创建CSS3动画的一些jQuery插件. 1. jQuery Smoove Smoove 简化了CSS3转换效果.使得页 ...

  3. Qt设置按钮为圆形

    通过Qt 的样式表实现圆形按钮,其也可以实现圆角按钮,当然也可以使用其他的方式,比如说,通过派生按钮类使用绘图事件,进行一个图形的绘制,或者是通过自定义一个类,通过信号与槽的机制与绘图事件的配合也能实 ...

  4. Unity中的动画系统和Timeline(2) 按钮动画和2D精灵动画

    按钮动画 1 创建按钮后,按钮的Button组件中,Transition我们平时用的时Tint,这次选择Animation 选择Auto Generate Animation,创建一个按钮动画 2 后 ...

  5. Linux下Qt创建共享库与链接共享库详解

    随着程序写的逐渐变多,或多或少的我们都会使用别人写好的库:或者我们不想让别人看到我们的一些核心程序,可以将核心程序封装成库.本次和大家分享的是在Ubuntu下使用Qt生成共享库以及在Qt中链接共享库的 ...

  6. jQuery Mobile 中创建按钮

    在 jQuery Mobile 中创建按钮 jQuery Mobile 中的按钮可通过三种方法创建: 使用 <button> 元素 使用 <input> 元素 使用 data- ...

  7. Web动画API教程1:创建基本动画

    本人转载自: Web动画API教程1:创建基本动画

  8. 5个基于css3超炫的鼠标滑动按钮动画

    今天给大家分享5个基于css3超炫的鼠标滑动按钮动画.这5个按钮鼠标经过的时候有超炫的动画效果.这5个按钮适用浏览器:360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之 ...

  9. 8款超酷而实用的CSS3按钮动画

    1.CSS3分享按钮动画特效 这是一款基于CSS3的社会化分享按钮,按钮非常简单,提供了分享到twitter.facebook.youtube等大型社交网站.每一个分享按钮都有个大社交网站的Logo图 ...

随机推荐

  1. Javaweb-IDEA 中Maven的操作

    1. 在idea中使用Maven 启动idea 创建一个MavenWeb项目 3.等待项目初始化完毕 4. 观察maven仓库中多了哪些东西 5. idea中的maven设置 注意:idea项目创成功 ...

  2. SAP APO-需求计划

    需求计划可以对市场中的产品进行预测. 需求计划过程的输出就是需求计划,它考虑了影响需求的所有因素. 需求计划流程定义了需求计划周期中的活动. 由于需求计划过程以循环的形式进行,因此可以重复某些活动. ...

  3. python小题目练习(十二)

    题目:如下图所示 代码展示: """Author:mllContent:春节集五福Date:2020-01-17"""import rand ...

  4. maven编译 出现Process terminated

    问题: 解决方案: 在Settings中配置一下maven

  5. NC15979 小q的数列

    NC15979 小q的数列 题目 题目描述 小q最近迷上了各种好玩的数列,这天,他发现了一个有趣的数列,其递推公式如下: \[f[i] = \left\{ \begin{array}{l} 0 ,&a ...

  6. [ARC087D] Squirrel Migration 补题记录

    题目链接 简要题意: 给你一个\(N\)个节点的树,求一个\(1\cdots N\)的排列\((p_1,p_2,\cdots p_N)\) ,使得\(\sum dist(i,p_i)\)最大. 求这样 ...

  7. Collection集合和Collection集合常用功能

    Collection集合常用功能 方法: boolean add(E e); 向集合中添加元素 boolean remove(E e); 删除集合中的某个元素 void clear(); 清空集合所有 ...

  8. java 九九乘法表(for循环)

    package study5ran2yl.study; public class ForDemo01 { public static void main(String[] args) { int h; ...

  9. 解决linuxdeployqt报错——系统版本过新的问题

    参考文章:https://icode.best/i/45016240865860 目前测试有效 大概你会跳转到这个议题 issues#340 显示这样类似的报错 linuxdeployqt 5 (co ...

  10. ATM系统开发(Java版)

    ATM系统开发 技术点分析 1.面向对象编程 每个用户的账户都是一个对象,所以需要设计账户类Accent用于创建账户对象封装账户信息. 2.使用集合容器 系统需要提供一个容器用于存储这些账户对象的信息 ...