zoukankan      html  css  js  c++  java
  • Qt-QPropertyAnimation实现的动画按钮

    相关资料:

    https://blog.csdn.net/weixin_42837024/article/details/93986970    Qt 自定义动画按钮(超酷炫)

    .pro

     1 QT       += core gui
     2 
     3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
     4 
     5 CONFIG += c++11
     6 
     7 # The following define makes your compiler emit warnings if you use
     8 # any Qt feature that has been marked deprecated (the exact warnings
     9 # depend on your compiler). Please consult the documentation of the
    10 # deprecated API in order to know how to port your code away from it.
    11 DEFINES += QT_DEPRECATED_WARNINGS
    12 
    13 # You can also make your code fail to compile if it uses deprecated APIs.
    14 # In order to do so, uncomment the following line.
    15 # You can also select to disable deprecated APIs only up to a certain version of Qt.
    16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    17 
    18 SOURCES += 
    19     main.cpp 
    20     mainwindow.cpp 
    21     myanimationbutton.cpp
    22 
    23 HEADERS += 
    24     mainwindow.h 
    25     myanimationbutton.h
    26 
    27 FORMS += 
    28     mainwindow.ui
    29 
    30 # Default rules for deployment.
    31 qnx: target.path = /tmp/$${TARGET}/bin
    32 else: unix:!android: target.path = /opt/$${TARGET}/bin
    33 !isEmpty(target.path): INSTALLS += target
    34 
    35 RESOURCES += 
    36     resource.qrc
    View Code

    main.cpp

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

    mainwindow.h

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

    mainwindow.cpp

     1 #include "mainwindow.h"
     2 #include "ui_mainwindow.h"
     3 
     4 MainWindow::MainWindow(QWidget *parent)
     5     : QMainWindow(parent)
     6     , ui(new Ui::MainWindow)
     7 {
     8     ui->setupUi(this);
     9 
    10     this->setWindowTitle(QStringLiteral("动画按钮"));
    11     myAnimationButton *owidget = new myAnimationButton(this);
    12     owidget->setImagePath(":/new/prefix1/wx.jpg");
    13     owidget->setText(QStringLiteral("微信支付"));
    14     owidget->setGeometry(0,0,400,400);
    15 
    16 //    ui->widget_2->setImagePath(":/pay.png");
    17 //    ui->widget_2->setText("支付宝支付");
    18 
    19 //    ui->widget_3->setImagePath(":/bank.png");
    20 //    ui->widget_3->setText("银行卡支付");
    21 }
    22 
    23 MainWindow::~MainWindow()
    24 {
    25     delete ui;
    26 }
    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>451</width>
    10     <height>437</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>451</width>
    23      <height>23</height>
    24     </rect>
    25    </property>
    26   </widget>
    27   <widget class="QStatusBar" name="statusbar"/>
    28  </widget>
    29  <resources/>
    30  <connections/>
    31 </ui>
    View Code

    myanimationbutton.h

     1 #ifndef MYANIMATIONBUTTON_H
     2 #define MYANIMATIONBUTTON_H
     3 
     4 #include <QPropertyAnimation>
     5 #include <QPainter>
     6 #include <QDebug>
     7 #include <QMouseEvent>
     8 #include <QWidget>
     9 
    10 class QPropertyAnimation;
    11 
    12 class myAnimationButton : public QWidget
    13 {
    14     Q_OBJECT
    15 public:
    16     explicit myAnimationButton(QWidget *parent = 0);
    17     ~myAnimationButton();
    18 
    19     void enterImageChanged(QVariant value);
    20     void leaveImageChanged(QVariant value);
    21 
    22     void setImagePath(QString path);
    23     void setText(QString text){
    24         this->text = text;
    25     }
    26 protected:
    27     void enterEvent(QEvent *event);
    28     void leaveEvent(QEvent *event);
    29     void paintEvent(QPaintEvent *event);
    30     void mousePressEvent(QMouseEvent *event);
    31 private:
    32 
    33     int imageWidth;
    34     int imageHeight;
    35 
    36     int originalWidth;
    37     int originalHeight;
    38 
    39     int newWidth;
    40     int newHeight;
    41 
    42     QString imagePath;
    43     QString text;
    44 
    45     QPropertyAnimation* enterAnimation;
    46     QPropertyAnimation* leaveAnimation;
    47 };
    48 
    49 #endif // MYANIMATIONBUTTON_H
    View Code

    myanimationbutton.cpp

      1 #include "myanimationbutton.h"
      2 
      3 myAnimationButton::myAnimationButton(QWidget *parent) :
      4     QWidget(parent)
      5 
      6 {
      7 
      8     imageWidth = 0;
      9     imageHeight = 0;
     10     originalWidth = 0;
     11     originalHeight = 0;
     12     newWidth = 0;
     13     newHeight = 0;
     14     imagePath = "";
     15 
     16 
     17     enterAnimation = new QPropertyAnimation(this,"");
     18     enterAnimation->setStartValue(0);
     19     enterAnimation->setEndValue(25);
     20     enterAnimation->setDuration(200);
     21     connect(enterAnimation,&QPropertyAnimation::valueChanged,
     22             this,&myAnimationButton::enterImageChanged);
     23 
     24     leaveAnimation = new QPropertyAnimation(this,"");
     25     leaveAnimation->setStartValue(0);
     26     leaveAnimation->setEndValue(25);
     27     leaveAnimation->setDuration(200);
     28     connect(leaveAnimation,&QPropertyAnimation::valueChanged,
     29             this,&myAnimationButton::leaveImageChanged);
     30 }
     31 
     32 myAnimationButton::~myAnimationButton()
     33 {
     34 
     35     delete enterAnimation;
     36     delete leaveAnimation;
     37 }
     38 
     39 void myAnimationButton::enterImageChanged(QVariant value)
     40 {
     41     newWidth = imageWidth + value.toInt();
     42     newHeight = imageHeight + value.toInt();
     43     update();
     44 }
     45 
     46 void myAnimationButton::leaveImageChanged(QVariant value)
     47 {
     48     newWidth = imageWidth - value.toInt();
     49     newHeight = imageHeight - value.toInt();
     50     update();
     51 }
     52 
     53 void myAnimationButton::setImagePath(QString path)
     54 {
     55     imagePath = path;
     56     QPixmap img(path);
     57     imageWidth = img.width();
     58     imageHeight = img.height();
     59     originalWidth = imageWidth;
     60     originalHeight = imageHeight;
     61     newWidth = imageWidth - 25;
     62     newHeight = imageHeight - 25;
     63     update();
     64 }
     65 
     66 void myAnimationButton::enterEvent(QEvent *event)
     67 {
     68 
     69     imageWidth = imageWidth - 25;
     70     imageHeight = imageHeight - 25;
     71     enterAnimation->start();
     72 }
     73 
     74 void myAnimationButton::leaveEvent(QEvent *event)
     75 {
     76 
     77     imageWidth = originalWidth;
     78     imageHeight = originalHeight;
     79     leaveAnimation->start();
     80 }
     81 
     82 void myAnimationButton::paintEvent(QPaintEvent *event)
     83 {
     84     if(imagePath.isEmpty())
     85         return;
     86 
     87     QPainter paint(this);
     88     paint.setRenderHint(QPainter::Antialiasing);
     89 
     90     QPixmap img(imagePath);
     91     img = img.scaled(newWidth,newHeight,Qt::IgnoreAspectRatio,
     92                      Qt::SmoothTransformation);
     93 
     94     int pixX = rect().center().x() - newWidth / 2;
     95     int pixY = rect().center().y() - newHeight / 2 - 10;
     96 
     97     QPoint point(pixX, pixY);
     98     paint.drawPixmap(point, img);
     99     paint.drawText(QRectF(0, height() - 20, width(), 20), Qt::AlignCenter, text);
    100 }
    101 
    102 void myAnimationButton::mousePressEvent(QMouseEvent *event)
    103 {
    104     if(imagePath.isEmpty())
    105         return;
    106 
    107     QPixmap image(imagePath);
    108     if(image.rect().contains(event->pos()))
    109     {
    110         qDebug()<<"press";
    111     }
    112 
    113 }
    View Code

    resource.qrc(不提供)

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

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

  • 相关阅读:
    jQuary学习の三の效果展示
    jQuary学习の二の语法
    jQuary学习の一の初期准备
    js备战春招の四のjs函数
    javascript JSON
    js备战春招の四の表单
    js备战春招の四の严格模式
    290. Word Pattern--Easy
    546. Remove Boxes--Hard
    牛客网-华为-2020届校园招聘上机考试-3
  • 原文地址:https://www.cnblogs.com/FKdelphi/p/14743610.html
Copyright © 2011-2022 走看看