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. shell 问题记录

    工作中写了个 RestAPI 接口,然后想通过 crontab 任务,去定时调用接口.发现去拼接 post 请求真的不容易.对于单引号,双引号的使用.很懵,示例代码如下:对于 '$line' 处,单引 ...

  2. web自动化之元素定位篇

    一.web自动化元素定位的方式有8种.------腾讯课堂 1.1 id定位: 1.2 class定位: 1.3 classname定位 1.4 tag_name 1.5

  3. 一分钟入门 Babel(下一代 JavaScript 语法的编译器)

    简单来说把 JavaScript 中 es2015/2016/2017/2046 的新语法转化为 es5,让低端运行环境(如浏览器和 node )能够认识并执行.严格来说,babel 也可以转化为更低 ...

  4. SAP Smart Form 无法通过程序自定义默认打印机问题解决

    *&---------------------------------------------------------------------* *& Form FRM_SET_PRI ...

  5. 几百行代码实现一个 JSON 解析器

    前言 之前在写 gscript时我就在想有没有利用编译原理实现一个更实际工具?毕竟真写一个语言的难度不低,并且也很难真的应用起来. 一次无意间看到有人提起 JSON 解析器,这类工具充斥着我们的日常开 ...

  6. Python快速下载商品数据,并连接数据库,保存数据

    开发环境 python 3.8 pycharm 2021.2 专业版 代码实现 发送请求 获取数据 解析数据(筛选数据) 保存数据 连接数据库 开始代码 请求数据 # 伪装 headers = { ' ...

  7. IntelliJ IDEA 项目文件旁边都有0%classes,0% lines covered

    IntelliJ IDEA 项目文件旁边都有0%classes,0% lines covered,解决方法:http://yayihouse.com/yayishuwu/chapter/2247

  8. hs-black 杂题选讲

    [POI2011]OKR-Periodicity 考虑递归地构造,设 \(\text{solve(s)}\) 表示字典序最小的,\(\text{border}\) 集合和 \(S\) 的 \(\tex ...

  9. 【转载】vscode配置C/C++环境

    VScode中配置 C/C++ 环境 Tip:请在电脑端查看 @零流@火星动力猿 2022.4.12 1. 下载编辑器VScode 官网:https://code.visualstudio.com/( ...

  10. mybatis查询的三种方式

    查询最需要关注的问题:①resultType自动映射,②方法返回值:  interface EmpSelectMapper: package com.atguigu.mapper; import ja ...