zoukankan      html  css  js  c++  java
  • Qt-Qt实现动画按钮(多图动画)

    相关资料:

    https://blog.csdn.net/qq_41399894/article/details/93483507?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-2&spm=1001.2101.3001.4242

     https://download.csdn.net/download/zhujianqiangqq/18466204    代码包下载

    .pro

     1 #-------------------------------------------------
     2 #
     3 # Project created by QtCreator 2019-06-24T11:25:07
     4 #
     5 #-------------------------------------------------
     6 
     7 QT       += core gui
     8 
     9 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    10 
    11 TARGET = untitled
    12 TEMPLATE = app
    13 
    14 # The following define makes your compiler emit warnings if you use
    15 # any feature of Qt which as been marked as deprecated (the exact warnings
    16 # depend on your compiler). Please consult the documentation of the
    17 # deprecated API in order to know how to port your code away from it.
    18 DEFINES += QT_DEPRECATED_WARNINGS
    19 
    20 # You can also make your code fail to compile if you use deprecated APIs.
    21 # In order to do so, uncomment the following line.
    22 # You can also select to disable deprecated APIs only up to a certain version of Qt.
    23 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    24 
    25 
    26 SOURCES += main.cpp
    27         mainwindow.cpp 
    28     mybtn.cpp
    29 
    30 HEADERS  += mainwindow.h 
    31     mybtn.h
    32 
    33 FORMS    += mainwindow.ui
    34 
    35 RESOURCES += 
    36     image.qrc
    View Code

    main.h

     1 #include "mainwindow.h"
     2 #include <QApplication>
     3 
     4 int main(int argc, char *argv[])
     5 {
     6     QApplication a(argc, argv);
     7     MainWindow w;
     8     w.show();
     9 
    10     return a.exec();
    11 }
    View Code

    mainwindow.h

     1 #ifndef MAINWINDOW_H
     2 #define MAINWINDOW_H
     3 
     4 #include <QMainWindow>
     5 namespace Ui {
     6 class MainWindow;
     7 }
     8 
     9 class MainWindow : public QMainWindow
    10 {
    11     Q_OBJECT
    12 
    13 public:
    14     explicit MainWindow(QWidget *parent = 0);
    15     ~MainWindow();
    16 
    17 private:
    18     Ui::MainWindow *ui;
    19 
    20 };
    21 
    22 #endif // MAINWINDOW_H
    View Code

    mainwindow.cpp

     1 #include "mainwindow.h"
     2 #include "ui_mainwindow.h"
     3 #include "mybtn.h"
     4 
     5 MainWindow::MainWindow(QWidget *parent) :
     6     QMainWindow(parent),
     7     ui(new Ui::MainWindow)
     8 {
     9     ui->setupUi(this);
    10     mainButton* m_btn=new mainButton(":/clean_Hover.png",":/clean_Leave.png",this);
    11     m_btn->setGeometry(30,30,95,95);
    12 }
    13 
    14 MainWindow::~MainWindow()
    15 {
    16     delete ui;
    17 }
    View Code

    mainwindow.ui

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <ui version="4.0">
     3  <class>MainWindow</class>
     4  <widget class="QMainWindow" name="MainWindow">
     5   <property name="geometry">
     6    <rect>
     7     <x>0</x>
     8     <y>0</y>
     9     <width>400</width>
    10     <height>300</height>
    11    </rect>
    12   </property>
    13   <property name="windowTitle">
    14    <string>MainWindow</string>
    15   </property>
    16   <widget class="QWidget" name="centralWidget"/>
    17   <widget class="QMenuBar" name="menuBar">
    18    <property name="geometry">
    19     <rect>
    20      <x>0</x>
    21      <y>0</y>
    22      <width>400</width>
    23      <height>23</height>
    24     </rect>
    25    </property>
    26   </widget>
    27   <widget class="QToolBar" name="mainToolBar">
    28    <attribute name="toolBarArea">
    29     <enum>TopToolBarArea</enum>
    30    </attribute>
    31    <attribute name="toolBarBreak">
    32     <bool>false</bool>
    33    </attribute>
    34   </widget>
    35   <widget class="QStatusBar" name="statusBar"/>
    36  </widget>
    37  <layoutdefault spacing="6" margin="11"/>
    38  <resources/>
    39  <connections/>
    40 </ui>
    View Code

    mybtn.h

     1 #ifndef MYBTN_H
     2 #define MYBTN_H
     3 
     4 #include <QObject>
     5 #include <QWidget>
     6 #include<QPaintEvent>
     7 #include<QEvent>
     8 #include<QPushButton>
     9 #include<qpropertyanimation.h>
    10 #include<QDebug>
    11 class mainButton : public QPushButton//用于主的图片
    12 {
    13     Q_OBJECT
    14 public:
    15     mainButton(QString pixenter, QString pixleave, QWidget*parent);
    16     ~mainButton();
    17 protected:
    18     //鼠标进入事件
    19     void enterEvent(QEvent*);
    20     //鼠标离开事件
    21     void leaveEvent(QEvent*);
    22     //绘制动态图片
    23     void paintEvent(QPaintEvent*);
    24     //两个动画类:一个进入一个离开
    25     QPropertyAnimation*m_enteranimation;
    26     QPropertyAnimation*m_leaveanimation;
    27     //存储进入、离开事件需要绘画的图片
    28     QList<QPixmap> m_enterlist;
    29     QList<QPixmap> m_leavelist;
    30     //进入事件绘制的图片下标
    31     int m_enterIndex;
    32     //离开事件绘制的图片下标
    33     int m_leaveIndex;
    34     //标志位
    35     bool m_enter;
    36     bool m_leave;
    37 public slots:
    38     void entervaluechange(QVariant var){m_enterIndex=var.toInt();update();}
    39     void leavevaluechange(QVariant var){m_leaveIndex=var.toInt();update();}
    40 };
    41 
    42 #endif // MYBTN_H
    View Code

    mybtn.cpp

     1 #include "mybtn.h"
     2 #include<QPainter>
     3 #include<QDebug>
     4 #include<QLabel>
     5 #include<QHBoxLayout>
     6 #include<QFontMetrics>
     7 mainButton::mainButton(QString strpixenter,QString strpixleave,QWidget*parent):QPushButton(parent)
     8 {
     9     QPixmap pixenter(strpixenter);
    10     QPixmap pixleave(strpixleave);
    11 
    12     m_leave=false;
    13     m_enter=true;
    14     m_leaveIndex=0;
    15     m_enterIndex=0;
    16     for(int i=0;i<10;i++)//进入
    17     {
    18         m_enterlist<<pixenter.copy(i*(pixenter.width()/10),0,pixenter.width()/10,pixenter.height());
    19     }
    20     for(int j=0;j<8;j++)//离开
    21     {
    22         m_leavelist<<pixleave.copy(j*(pixleave.width()/8),0,pixleave.width()/8,pixleave.height());
    23     }
    24     //QPropertyAnimation的效果就是把绑定的变量从设定的初始值变为结束值
    25     m_enteranimation=new QPropertyAnimation(this,"");
    26     m_enteranimation->setStartValue(0);
    27     m_enteranimation->setEndValue(9);
    28     //动画的持续时间
    29     m_enteranimation->setDuration(600);
    30     connect(m_enteranimation,SIGNAL(valueChanged(QVariant)),this,SLOT(entervaluechange(QVariant)));
    31 
    32     m_leaveanimation=new QPropertyAnimation(this,"");
    33     m_leaveanimation->setStartValue(0);
    34     m_leaveanimation->setEndValue(7);
    35     m_leaveanimation->setDuration(600);
    36     connect(m_leaveanimation,SIGNAL(valueChanged(QVariant)),this,SLOT(leavevaluechange(QVariant)));
    37 }
    38 mainButton::~mainButton()
    39 {
    40     delete m_leaveanimation;
    41     delete m_enteranimation;
    42 }
    43 void mainButton::enterEvent(QEvent *)
    44 {
    45     m_enter=true;
    46     m_leave=false;
    47     m_enteranimation->start();
    48 }
    49 void mainButton::leaveEvent(QEvent *)
    50 {
    51     m_enter=false;
    52     m_leave=true;
    53     m_leaveanimation->start();
    54 }
    55 void mainButton::paintEvent(QPaintEvent *)
    56 {
    57     QPainter painter(this);
    58     if(m_enter)
    59     painter.drawPixmap(rect(),m_enterlist.at(m_enterIndex));
    60     if(m_leave)
    61     painter.drawPixmap(rect(),m_leavelist.at(m_leaveIndex));
    62 }
    View Code

    resource.qrc(不提供)

    作者:疯狂Delphi
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.

    欢迎关注我,一起进步!扫描下方二维码即可加我

  • 相关阅读:
    树莓派3B+通过路由器进SSH和VNC
    vue 构造函数的 add 方法
    jQuery选择器-->属性选择器
    jQuery选择器-->过滤选择器之表单子元素过滤器
    jQuery选择器-->过滤选择器之表单对象的属性过滤器
    jQuery选择器-->过滤选择器之可见性过滤器
    jQuery选择器-->过滤选择器之内容过滤器
    jQuery选择器-->过滤选择器之简单过滤器
    jQuery选择器-->层次选择器之prev~siblings选择器
    jQuery选择器-->层次选择器之prev+next选择器
  • 原文地址:https://www.cnblogs.com/FKdelphi/p/14744316.html
Copyright © 2011-2022 走看看