QT 制作图片旋转、反转

参考链接: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 

namespace Ui {
class flashItem;
}

class flashItem : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::flashItem *ui;
};

#include 
#include 
#include 
#include 
#include 
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 
#include 
#include 

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 

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

    return a.exec();
}

ui文件(flashitem.ui)



 flashItem
 
  
   
    0
    0
    884
    529
   
  
  
   flashItem
  
  
  
   
    
     0
     0
     884
     25
    
   
  
  
   
    TopToolBarArea
   
   
    false
   
  
  
 
 
 
 

资源文件(.qrc)


    
        images/rotate.png
        
    

你可能感兴趣的:(QT,qt,linux)