QT学习-----按钮弹起效果的实现
生活随笔
收集整理的這篇文章主要介紹了
QT学习-----按钮弹起效果的实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
策略打錯了。將錯就錯吧。記錄一下這個按鈕彈起效果的實現,以方便自己以后的使用
創建一個Qt項目,不寫了。
(設置屏幕的大小、標題、標題圖片)
//設置固定的大小this->setFixedSize(320,588);//設置標題this->setWindowTitle("瘋瘋翻金幣");//設置圖片this->setWindowIcon(QIcon("://Image/Coin0001.png"));重寫paintEvent事件(加背景圖片,寫在mainwindow.h,實現在這個的cpp)
定義
void paintEvent(QPaintEvent *event);實現
void MainWindow::paintEvent(QPaintEvent *event) {QPainter painter(this);QPixmap pix;pix.load("://Image/PlayLevelSceneBg.png");painter.drawPixmap(0,0,this->width(),this->height(),pix);//添加標題pix.load("://Image/Title.png");pix = pix.scaled(pix.width()*0.5,pix.height()*0.5);painter.drawPixmap(10,10,pix);}創建MyPushButton類
MyPushButoon.h(最終)
#ifndef MYPUSHBUTTON_H #define MYPUSHBUTTON_H#include <QPushButton> #include <QDebug>class MyPushButton : public QPushButton {Q_OBJECT public: // explicit MyPushButton(QWidget *parent = nullptr);MyPushButton(QString normalImg,QString pressImg = "");QString normalImgPath;QString pressImgPath;//向下跳躍void Zoom1();//向上跳躍void Zoom2(); signals:public slots: };#endif // MYPUSHBUTTON_HMyPushButton.cpp(最終)
#include "mypushbutton.h" #include <QPropertyAnimation> MyPushButton::MyPushButton(QString normalImg, QString pressImg) {this->normalImgPath = normalImg;this->pressImgPath = pressImg;QPixmap pix;bool ret = pix.load(this->normalImgPath);if(!ret){qDebug()<<"圖片加載失敗"<<this->normalImgPath;return ;}//設置圖片的大小this->setFixedSize(pix.width(),pix.height());//樣式this->setStyleSheet("QPushButton{border:0px;}");//設置圖標this->setIcon(pix);//設置圖片的大小this->setIconSize(QSize(pix.width(),pix.height()));}void MyPushButton::Zoom1() {QPropertyAnimation *animation = new QPropertyAnimation(this,"geometry");//設置動畫時間間隔animation->setCurrentTime(200);//設置動畫對象起始位置animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));//設置動畫對象結束位置animation->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));//設置曲線animation->setEasingCurve(QEasingCurve::OutBounce);//執行動畫animation->start(); }void MyPushButton::Zoom2() {QPropertyAnimation *animation = new QPropertyAnimation(this,"geometry");//設置動畫時間間隔animation->setCurrentTime(200);//設置動畫對象起始位置animation->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));//設置動畫對象結束位置animation->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));//設置曲線animation->setEasingCurve(QEasingCurve::OutBounce);//執行動畫animation->start(); }在MainWindow里面追加信號槽,調用彈起的兩個函數
MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow) {ui->setupUi(this);//設置固定的大小this->setFixedSize(320,588);//設置標題this->setWindowTitle("瘋瘋翻金幣");//設置圖片this->setWindowIcon(QIcon("://Image/Coin0001.png"));//設置固定的大小this->setFixedSize(320,588);//設置標題this->setWindowTitle("瘋瘋翻金幣");//設置圖片this->setWindowIcon(QIcon("://Image/Coin0001.png"));//開始按鈕的創建MyPushButton *startBtn = new MyPushButton("://Image/MenuSceneStartButton.png");startBtn->setParent(this);startBtn->move(this->width()*0.5-startBtn->width()*0.5,this->height()*0.7);connect(startBtn,&MyPushButton::clicked,[=](){//點擊按鈕有彈跳效果startBtn->Zoom1();startBtn->Zoom2();}); }總結
以上是生活随笔為你收集整理的QT学习-----按钮弹起效果的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java 支付宝手机网站支付下单 支付回
- 下一篇: C/C++模拟按键按下弹起