QT 制作图片旋转、反转

2023-05-16

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

QT 制作图片旋转、反转 的相关文章

  • 设置xrdp使用固定的会话

    修改xrdp ini配置文件 xff0c 位于 etc xrdp xrdp ini 要打开和编辑xrdp的配置文件 xff0c 请使用 xff1a sudo nano etc xrdp xrdp ini 默认情况下 xff0c 第一个xrd
  • 回想当年 91d2 1588 神戒

    渡鳥之箍 全JP属性 xff0c 1588成交
  • mysql 事务 rollback 失效

    最近支付业务发生了一件怪事 xff0c 在一个事务 失败后 xff0c 调用rollback xff0c 发现只rollback 了最后一条sql 开发查了很久 xff0c 发现是网络抖动 xff0c 造成第一次创建链接的 begin 丢掉
  • 旅游

    每次出游 我们自己看来 别人看来 实际上
  • 使用cgroup 限制进程的磁盘io

    cat etc cgconfig conf Copyright IBM Corporation 2007 Authors Balbir Singh lt balbir 64 linux vnet ibm com gt This progra
  • mac ox ssd 开启 trim

    使用clover 补丁开启
  • aerospike参考资料

    本文转自 xff1a http blog csdn net songhuiqiao article details 50324073 aerospike QQ群 xff1a 419183757 http blog csdn net jias
  • aerospike init

    本文转自 xff1a http blog csdn net songhuiqiao article details 50324109 aerospike QQ群 xff1a 419183757 aerospike init 1 aerosp
  • 文章标题

    本文转自 xff1a http blog csdn net songhuiqiao article details 50324139 aerospike QQ群 xff1a 419183757 Distribution 分布 Aerospi
  • 源码编译aerospike-server-3.6.4

    生产环境一直使用3 5 3这个版本 xff0c 和开发商量了一下 xff0c 在测试环境使用3 6 4搭建集群 xff0c 如果运行稳定就找机会将生产环境的集群升级 aerospike QQ群 xff1a 419183757 从github
  • 暗黑破坏神 2 私服 sf 114.215.178.67

    注册表 REGEDIT4 HKEY CURRENT USER Software Blizzard Entertainment Diablo II 34 BNETIP 34 61 34 114 215 178 67 34 1 11b 原版 Q
  • Linux下NVIDIA驱动手动安装

    1 查看当前电脑的显卡型号 lshw numeric C display 执行完毕后我的显卡型号为 GTX 960M xff1a 2 下载NVIDIA官方驱动 到NVIDIA的官方驱动网站下载对应显卡的驱动程序 xff0c 下载后的文件格式
  • mysql checksum table golang

    package main import 34 bytes 34 34 database sql 34 34 fmt 34 34 github com go sql driver mysql 34 34 io ioutil 34 34 reg
  • golang context.WithTimeout 超时处理

    lt span style 61 34 font size 18px color 3333ff 34 gt package main import 34 golang org x net context 34 34 log 34 34 ma
  • supervisord 启动后 ulimit 参数未生效

    bin bash supervisord This scripts turns supervisord on Author Mike McGrath lt mmcgrath 64 redhat com gt based off yumupd
  • postgresql 定时vacuum脚本

    coding utf 8 import pg multiprocessing lst 61 34 dbname 34 34 postgres 34 34 host 34 34 127 0 01 34 34 port 34 5432 34 u
  • 空闲时间的处理:OnIdle,以消息循环过程中为例(顺便解释PeekMessage与GetMessage的不同)

    所谓空闲时间 xff08 idle time xff09 xff0c 是指 系统中没有任何消息等待处理 的时间 举个例子 xff0c 没有任何程序使用定时器 xff08 timer xff0c 它会定时送来WM TIMER xff09 xf
  • 七层网络学习

    七层网络协议 OSI协议 应用层 表示层 会话层 传输层 网络层 数据链路层 物理层 传输层 xff1a TCP IP协议 xff0c 就是在数据包外面在加一层 xff0c 加的数据是源端口和目标端口 xff1b 网络层 xff1a 在数据
  • 生成m个长度在n以内的随机字符串

    开发环境 xff1a VS Code xff08 1 45 1 xff09 include lt stdio h gt include lt stdlib h gt include lt time h gt define M 15 defi
  • windows下MFC定时器开发学习

    在VS2010中新建一个项目 xff0c Visual C 43 43 gt MFC gt MFC应用程序 xff0c 命名为 Timer gt 确定 xff0c 选择MFC向导建立基于对话框 xff1b 2 在自动生成的对话框模板中 xf

随机推荐

  • MFC 获取与更新控件

    EDIT控件 xff1a 获取控件值 xff1a CString str GetDlgItem IDC EDIT1 gt GetWindowText str IDC EDIT1为控件ID 更新控件值 xff1a CString str 61
  • TypeError: Expected ‘Iterator‘ as the return annotation for __iter__ of ExperienceSourceDataset

    问题 xff1a 使用pl bolts时产生错误 TypeError Expected 39 Iterator 39 as the return annotation for iter of ExperienceSourceDataset
  • 为什么调用了KillTimer()函数后,还是会进入OnTimer函数?

    今天写MFC定时器代码时 xff0c 发现调用了KillTimer 函数后 xff0c 还是会进入OnTimer函数 实现如下 xff1a 通过按钮控制定时器的开关 按钮回调函数 void CtimerDlg OnBnClickedButt
  • linux GDB调试

    前言 GDB调试 xff08 GNU debug xff09 是unix下的调试工具 xff0c 可以调试C和C 43 43 xff1b 程序怎么才能使用GDB xff1f 编译的时候加上 g xff0c 保留调试参数 xff1b 如果是别
  • 为什么基类的析构函数必须是虚函数

    因为当定义基类的指针指向子类对象时 xff0c 在调用析构函数的时候 xff0c 如果析构函数是虚函数 xff0c 那么就会调到子类的析构函数 xff0c 所以如果子类申请了新的内存的话 xff0c 那这块的内存就能被释放 xff1b 但是
  • python

    获取输入 获取string输入 xff1a str1 61 str input 获取数字输入 xff1a num1 61 int input
  • linux指令学习

    find 根据文件的名字查找 find name 34 list c 34 或者 find name 39 list c 39 xff1a 查找根目录下 xff0c 名字为list c的文件 grep 根据文件的内容查找 grep n he
  • windows快捷键

    1 非常实用的截图 xff1a shift 43 windows键 43 s
  • NLP思维

    前言 想让自己的思维有深度 xff0c NLP思维逻辑必须要会 xff01 通常在低层次的问题在高层次能轻易找到解决方法 xff0c 如果在同层次或者更低层次寻找解决问题的办法 xff0c 往往会消耗更多的精力 NLP思维分层 xff1a
  • linux不生成core dump文件

    今天尝试core dump功能调试 xff0c 发现一直没法生成崩溃dump文件 代码如下 xff1a include 34 iostream 34 using namespace std int main int a 61 10 cin
  • Windows下断点调试技巧

    添加普通断点 在代码行号左边空白处 xff0c 左键单击即可添加断点 xff1b 添加数据断点 xff08 监控数据变化 xff09 在需要监控的数据的行添加普通断点 xff1b 通过Debug模式运行软件 xff1b 等软件运行到需要监控
  • E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable)

    sudo apt get innstall时报错 E Could not get lock var lib dpkg lock frontend open 11 Resource temporarily unavailable E Unab
  • 【损失函数】图像分割损失CELoss中添加 OHEM

    语义分割中常用交叉熵损失CE xff0c 在应用中通常添加OHEM以获取更好的收敛 xff08 经验阈值是0 7 xff09 xff0c 这里OHEM思想的来源是topk loss xff0c 其介绍参考 论文 损失函数 Learning
  • 开发板ifconfig时,没有wlan0

    问题 xff1a 开发板起来后 xff0c 输入ifconfig指令时 xff0c 只能看到eth0和lo xff0c 没看到wlan0 xff1b 原因 xff1a 没有挂载wifi驱动模块 xff1b 具体操作如下 xff1a 输入ls
  • ubuntu下将开发板中的内容导出

    方法 xff1a 通过tftp xff0c 开发板是客户端 xff0c 电脑是服务器 步骤 xff1a 搭建tftp环境 xff0c 开发板默认有tftp客户端功能 xff0c 需要在电脑端搭建tftp服务器环境 xff1b 在开发板指令终
  • sh脚本文件运行方式和区别

    有如下shell脚本test sh bin bash b 61 10 echo b sh test sh 和 bash test sh 指令在一个新开的子shell终端执行 xff0c 也就是说sh脚本中的变量不会在当前终端生效 xff0c
  • Ubuntu安装多个版本QT后怎么修改执行qmake使用的QT版本?

    查看当前QT版本 xff1a 输入指令 qmake v 可以看到当前版本是QT 4 8 7 查看QT编译器选择配置文件 xff1a cd usr lib x86 64 linux gnu qt default qtchooser sudo
  • ubuntu qt 创建工程时 no walid kit

    sudo apt get install qt sdk 之后输入sudo apt get qt default 点击 options gt Build amp Run gt Kits gt Desktop gt Qt Version 选择Q
  • QT创建Qlabel控件后没有显示

    在主窗口创建qlabel控件后 xff0c 控件没有显示 问题原因 xff1a 没有指定控件的父对象 指定父对象后 xff0c 就能显示了
  • QT 制作图片旋转、反转

    参考链接 xff1a QGraphicsPixmapItem QPropertyAnimation QTransform 自定义图片控件旋转 缩放 图形视图框架 三 xff09 码农家园 codenong com 代码 xff1a 工程文件