Qt-QPropertyAnimation的使用(支持放大、移动、透明动画)
相关资料:
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 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 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 #include4 5 int main(int argc, char *argv[]) 6 { 7 QApplication a(argc, argv); 8 MainWindow w; 9 w.show(); 10 return a.exec(); 11 }
mainwindow.h
1 #ifndef MAINWINDOW_H 2 #define MAINWINDOW_H 3 4 #include5 #include 6 #include 7 #include 8 #include 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_H
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 }
mainwindow.ui
1 <?xml version="1.0" encoding="UTF-8"?> 2"4.0"> 3 <class>MainWindowclass> 4 class="QMainWindow" name="MainWindow"> 5 "geometry"> 6 137 120 80 9550 10400 11"windowTitle"> 14 <string>MainWindowstring> 15 16class="QWidget" name="centralwidget"/> 17 1819 20
搜索
复制