QT 制作图片旋转、反转

该代码示例展示了如何在Qt环境中使用QGraphicsPixmapItem、QPropertyAnimation和QTransform来创建自定义图片控件,实现图片的旋转和缩放动画效果。通过QGraphicsScene和QGraphicsView设置场景和视图,利用QPropertyAnimation控制图片的旋转角度,实现平滑循环的动画。
摘要由CSDN通过智能技术生成

参考链接:QGraphicsPixmapItem QPropertyAnimation QTransform 自定义图片控件旋转、缩放 图形视图框架( 三) | 码农家园 (codenong.com)代码:

工程文件(.pro)

#-------------------------------------------------
#
# Project created by QtCreator 2023-04-04T19:01:00
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = untitled1
TEMPLATE = app


SOURCES += main.cpp\
        flashitem.cpp

HEADERS  += flashitem.h

FORMS    += flashitem.ui

RESOURCES += \
    pict.qrc

头文件(flashitem.h)

#ifndef FLASHITEM_H
#define FLASHITEM_H

#include <QMainWindow>

namespace Ui {
class flashItem;
}

class flashItem : public QMainWindow
{
    Q_OBJECT

public:
    explicit flashItem(QWidget *parent = 0);
    ~flashItem();

private:
    Ui::flashItem *ui;
};

#include <QGraphicsItem>
#include <QGraphicsPixmapItem>
#include <QPainter>
#include <QTimer>
#include <QTransform>
class ItemClass : public QObject , public QGraphicsPixmapItem
{
    Q_OBJECT
    Q_PROPERTY(int RotateAngle READ RotateAngle WRITE setRotateAngle)
public:
    explicit ItemClass(QObject *parent = 0, const int& type = int());
    QRectF boundingRect() const override;

    int RotateAngle();
    void setRotateAngle(int angle);
private:
    int m_angle;
    int m_type;

};

#endif // FLASHITEM_H

源文件(flashitem.cpp)

#include "flashitem.h"
#include "ui_flashitem.h"
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QPropertyAnimation>

flashItem::flashItem(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::flashItem)
{
    ui->setupUi(this);

    QGraphicsScene *scene=new QGraphicsScene;
    // 创建200*200的scene,scene的中心和当前widget的中心位置重合,通过增加x,y值可以将scene位置向左上移动
    scene->setSceneRect(-200,-200,400,400);

    ItemClass *flashItem = new ItemClass(this,0);
    flashItem->setPixmap(QPixmap(":/rotate.png").scaled(200,200));
    flashItem->setFlag(QGraphicsItem::ItemIsMovable);
    flashItem->setPos(100,-100); // 在scene中心位置

    QPropertyAnimation *animation = new QPropertyAnimation(flashItem,"RotateAngle");
    animation->setStartValue(0);
    animation->setEndValue(360);
    animation->setDuration(2000);
    animation->setEasingCurve(QEasingCurve::Linear);
    animation->setLoopCount(-1);
    animation->start();

    ItemClass *flashItem1 = new ItemClass(this,1);
    flashItem1->setPixmap(QPixmap(":/rotate.png").scaled(200,200));
    flashItem1->setFlag(QGraphicsItem::ItemIsMovable);
    flashItem1->setPos(-100,0);// 通过增加x,y值可以将scene位置向左上移动

    QPropertyAnimation *animation1 = new QPropertyAnimation(flashItem1,"RotateAngle");
    animation1->setStartValue(0);
    animation1->setEndValue(360);
    animation1->setDuration(2000);
    animation1->setEasingCurve(QEasingCurve::Linear);
    animation1->setLoopCount(-1);
    animation1->start();

    ItemClass *flashItem2 = new ItemClass(this,2);
    flashItem2->setPixmap(QPixmap(":/rotate.png").scaled(200,200));
    flashItem2->setFlag(QGraphicsItem::ItemIsMovable);
    flashItem2->setPos(-100,-100);// 在scene(-100,-100)位置

    QPropertyAnimation *animation2 = new QPropertyAnimation(flashItem2,"RotateAngle");
    animation2->setStartValue(0);
    animation2->setEndValue(360);
    animation2->setDuration(2000);
    animation2->setEasingCurve(QEasingCurve::Linear);
    animation2->setLoopCount(-1);
    animation2->start();

    ItemClass *flashItem3 = new ItemClass(this,3);
    flashItem3->setPixmap(QPixmap(":/rotate.png").scaled(200,200));
    flashItem3->setFlag(QGraphicsItem::ItemIsMovable);
    flashItem3->setPos(0,0);//通过增加x,y值可以将scene位置向左上移动

    QPropertyAnimation *animation3 = new QPropertyAnimation(flashItem3,"RotateAngle");
    animation3->setStartValue(1);
    animation3->setEndValue(3);
    animation3->setDuration(2100);
    animation3->setEasingCurve(QEasingCurve::Linear);
    animation3->setLoopCount(-1);
    animation3->start();

    scene->addItem(flashItem);
    scene->addItem(flashItem1);
    scene->addItem(flashItem2);
    scene->addItem(flashItem3);


    QGraphicsView *view = new QGraphicsView(this);
    //view->setRenderHint(QPainter::Antialiasing);
    view->setScene(scene);
    view->setMinimumSize(width(),height());
    view->show();

    show();

}

flashItem::~flashItem()
{
    delete ui;
}

ItemClass::ItemClass(QObject *parent, const int &type)
{
    m_type = type;
}

QRectF ItemClass::boundingRect() const
{
    return QRectF(0,0,200,200);
}

int ItemClass::RotateAngle()
{
    return m_angle;
}

void ItemClass::setRotateAngle(int angle)
{
    m_angle = angle;
    if(m_type == 0)
        setTransform(QTransform().rotate(angle, Qt::XAxis));
    else if(m_type == 1)
        setTransform(QTransform().rotate(angle, Qt::YAxis));
    else if(m_type == 2){
        setTransform(QTransform().
                     translate(this->boundingRect().width()/2,this->boundingRect().height()/2).
                     rotate(angle, Qt::ZAxis).
                     translate(0-this->boundingRect().width()/2,0-this->boundingRect().height()/2));

    }else if(m_type == 3)
        setTransform(QTransform().scale(angle,angle));
}

主函数(main.cpp)

#include "flashitem.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    flashItem w;
    w.show();

    return a.exec();
}

ui文件(flashitem.ui)

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>flashItem</class>
 <widget class="QMainWindow" name="flashItem">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>884</width>
    <height>529</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>flashItem</string>
  </property>
  <widget class="QWidget" name="centralWidget"/>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>884</width>
     <height>25</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

资源文件(.qrc)

<RCC>
    <qresource>
        <file alias="rotate.png">images/rotate.png</file>
        <!--<file alias="cut-img2.png">images/cut2.png</file>-->
    </qresource>
</RCC>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值