Qt-QPropertyAnimation的使用(支援放大、移動、透明動畫)
阿新 • • 發佈:2022-05-20
相關資料:
https://download.csdn.net/download/zhujianqiangqq/85424062 csdn程式碼包下載
.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 warningsView Code9 # 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 22 HEADERS += \ 23 mainwindow.h 24 25 FORMS += \26 mainwindow.ui 27 28 # Default rules for deployment. 29 qnx: target.path = /tmp/$${TARGET}/bin 30 else: unix:!android: target.path = /opt/$${TARGET}/bin 31 !isEmpty(target.path): INSTALLS += target 32 33 RESOURCES += \ 34 resource.qrc
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 #include <QPropertyAnimation> 6 #include <QTimer> 7 #include <QLabel> 8 #include <QPushButton> 9 10 QT_BEGIN_NAMESPACE 11 namespace Ui { class MainWindow; } 12 QT_END_NAMESPACE 13 14 class MainWindow : public QMainWindow 15 { 16 Q_OBJECT 17 18 public: 19 MainWindow(QWidget *parent = nullptr); 20 ~MainWindow(); 21 22 private slots: 23 void on_Geometry(); 24 void on_Pos(); 25 void on_WindowOpacity(); 26 void on_Timer(); 27 void on_ButtonTimer(); 28 private: 29 Ui::MainWindow *ui; 30 QPropertyAnimation *m_animation;// 動畫物件指標 31 QTimer *m_pTimer; 32 QLabel *m_pLabel; 33 QPushButton *m_pGeometry; 34 QPushButton *m_pPos; 35 QPushButton *m_pWindowOpacity; 36 QPushButton *m_pButtonTimer; 37 }; 38 #endif // MAINWINDOW_HView 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 setWindowTitle(QStringLiteral("Qt之QPropertyAnimation的使用(支援放大、移動、透明動畫)")); 10 11 m_pLabel = new QLabel(this); 12 m_pLabel->setPixmap(QPixmap(":/new/prefix1/roommain.png")); 13 m_pLabel->setGeometry(100, 100, 100, 100); 14 m_pLabel->setScaledContents(true); 15 16 m_animation = new QPropertyAnimation(); 17 m_animation->setEasingCurve(QEasingCurve::Linear); //設定動畫效果 18 19 m_pTimer = new QTimer(this); 20 m_pTimer->setSingleShot(false); 21 connect(m_pTimer, &QTimer::timeout, this, &MainWindow::on_Timer); 22 23 m_pGeometry = new QPushButton(this); 24 m_pGeometry->setText(QStringLiteral("改大小")); 25 m_pGeometry->setGeometry(400, 10, 100, 25); 26 connect(m_pGeometry, &QPushButton::clicked, this, &MainWindow::on_Geometry); 27 28 m_pPos = new QPushButton(this); 29 m_pPos->setText(QStringLiteral("移動")); 30 m_pPos->setGeometry(400, 40, 100, 25); 31 connect(m_pPos, &QPushButton::clicked, this, &MainWindow::on_Pos); 32 33 m_pWindowOpacity = new QPushButton(this); 34 m_pWindowOpacity->setText(QStringLiteral("透明")); 35 m_pWindowOpacity->setGeometry(400, 70, 100, 25); 36 connect(m_pWindowOpacity, &QPushButton::clicked, this, &MainWindow::on_WindowOpacity); 37 38 m_pButtonTimer = new QPushButton(this); 39 m_pButtonTimer->setText(QStringLiteral("顯示座標")); 40 m_pButtonTimer->setGeometry(400, 100, 100, 25); 41 connect(m_pButtonTimer, &QPushButton::clicked, this, &MainWindow::on_ButtonTimer); 42 } 43 44 MainWindow::~MainWindow() 45 { 46 delete ui; 47 } 48 49 void MainWindow::on_Geometry() 50 { 51 // geometry:按矩形的動畫(移動和縮放) 52 m_animation->setTargetObject(m_pLabel); // 設定使用動畫的控制元件 53 m_animation->setPropertyName("geometry"); // 指定動畫屬性名 54 m_animation->setDuration(100); // 設定動畫時間(單位:毫秒) 55 // 獲取控制元件初始的大小 56 int width = m_pLabel->rect().width(); 57 int height = m_pLabel->rect().height(); 58 QPoint oPoint = m_pLabel->pos(); 59 // 設定動畫起始位置 60 m_animation->setStartValue(QRect(oPoint, QSize( width, height))); 61 // 設定動畫步長值,以及在該位置時的長寬 62 m_animation->setKeyValueAt(0.5, QRect(m_pLabel->pos() - QPoint(50, 50), QSize( width + 100, height + 100))); 63 // 設定動畫結束位置及其大小 64 m_animation->setEndValue(QRect(oPoint, QSize( width, height))); 65 // 啟動動畫 66 m_animation->start(); 67 } 68 69 void MainWindow::on_Pos() 70 { 71 // pos:按點移動的動畫(移動) 72 m_animation->setTargetObject(m_pLabel); // 設定使用動畫的控制元件 73 m_animation->setPropertyName("pos"); // 指定動畫屬性名 74 m_animation->setDuration(3000); // 設定動畫時間(單位:毫秒) 75 // 設定動畫起始位置在label控制元件當前的pos 76 m_animation->setStartValue(m_pLabel->pos()); 77 // 設定動畫結束位置 78 m_animation->setEndValue(m_pLabel->pos() + QPoint(200, 100)); 79 // 啟動動畫 80 m_animation->start(); 81 } 82 83 void MainWindow::on_WindowOpacity() 84 { 85 // windowOpacity:不透明度(注意該效果只對頂級視窗有效哦) 86 m_animation->setTargetObject(this); // 重設動畫使用物件 87 m_animation->setPropertyName("windowOpacity"); // 指定動畫屬性名 88 m_animation->setDuration(2000); // 設定動畫時間(單位:毫秒) 89 90 // 設定動畫步長值,以及在該位置時顯示的透明度 91 m_animation->setKeyValueAt(0, 1); 92 m_animation->setKeyValueAt(0.5, 0); 93 m_animation->setKeyValueAt(1, 0); 94 // 當值為-1時,動畫一直執行,直到視窗關閉 95 m_animation->setLoopCount(-1); 96 // 啟動動畫 97 m_animation->start(); 98 } 99 100 void MainWindow::on_Timer() 101 { 102 QString s = "x:%1 y:%2"; 103 setWindowTitle(s.arg(m_pLabel->pos().x()).arg(m_pLabel->pos().y())); 104 } 105 106 void MainWindow::on_ButtonTimer() 107 { 108 m_pTimer->start(100); 109 }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>550</width> 10 <height>400</height> 11 </rect> 12 </property> 13 <property name="windowTitle"> 14 <string>MainWindow</string> 15 </property> 16 <widget class="QWidget" name="centralwidget"/> 17 </widget> 18 <resources/> 19 <connections/> 20 </ui>View Code
搜尋
複製