QT 图片旋转--QTransform 沿中间旋转

#ifndef shadow_H_
#define shadow_H_
#include <QtGui/QtGui>
#include <QtCore/QtCore>

class ShadowWidget : public QWidget
{
    Q_OBJECT
public:
    ShadowWidget(QWidget *parent =0)
    {
        //setWindowFlags(Qt::FramelessWindowHint);
        QGraphicsDropShadowEffect *fEffect =  new QGraphicsDropShadowEffect;
        //fEffect->setColor(QColor(255,0,0));
        QVBoxLayout *layout = new QVBoxLayout(this);
        QGraphicsScene *scene = new QGraphicsScene;
        scene->setSceneRect(QRect(0,0,900,600));
         fPixmap= new QGraphicsPixmapItem;
        fPixmap->setPos(200,200);
        QPixmap *pix = new QPixmap("1.jpg");
        fPixmap->setPixmap(*pix);
        //fPixmap->setGraphicsEffect(fEffect);
        QTransform *tran = new QTransform;
        tran->rotate(80,Qt::YAxis);
        //fPixmap->setTransform(*tran);
        //fPixmap->setTransformOriginPoint(QPointF(pix->size().width()/2,pix->size().height()/2));
        //fPixmap->setTransformOriginPoint(100,100);
        scene->addItem(fPixmap);
        QGraphicsView *view = new QGraphicsView(scene);
        layout->addWidget(view);
        QPushButton * animButton = new QPushButton(tr("Start"));
        layout->addWidget(animButton);
        connect(animButton,SIGNAL(clicked()),this,SLOT(performAnimation()));
        //setGraphicsEffect(fEffect);
        resize(900,600);
        fTimeLine = NULL;
    }

private slots:
    void performAnimation()
    {
        if(!fTimeLine)
        {
            fTimeLine = new QTimeLine;
            fTimeLine->setDuration(1000);
            fTimeLine->setFrameRange(1,10);
            connect(fTimeLine,SIGNAL(frameChanged(int)),this,SLOT(performRotateAnimation(int )));
        }

        fTimeLine->start();
    }

    void performRotateAnimation(int frame)
    {
        qreal angle = 90*(frame)/10.0;
        QTransform tran;
        tran.translate(150,0);
        //fPixmap->setTransformOriginPoint(0,0);
        tran.rotate(angle,Qt::YAxis);
        fPixmap->setTransform(tran);
        QTransform t;
        t.translate(-150,0);
        fPixmap->setTransform(t,true);
    }
private:
    QGraphicsPixmapItem *fPixmap;
    QTimeLine *fTimeLine ;
};
#endif
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 `QTransform` 中的 `rotate` 方法实现让图片围绕 label 中心旋转的效果。具体步骤如下: 1. 获取 label 的中心点坐标,可以使用 `label.geometry().center()` 方法获取。 2. 构造一个 `QTransform` 对象,并使用 `rotate` 方法对其进行旋转操作。需要注意的是,`rotate` 方法需要传入旋转角度和旋转中心点坐标。 3. 将构造好的 `QTransform` 对象应用到图片上,可以使用 `QPainter` 中的 `setTransform` 方法。 示例代码如下: ```python from PyQt5.QtGui import QPixmap, QPainter, QTransform from PyQt5.QtWidgets import QLabel, QMainWindow class MyWindow(QMainWindow): def __init__(self): super().__init__() self.label = QLabel(self) self.label.setGeometry(50, 50, 200, 200) self.setCentralWidget(self.label) self.image = QPixmap("path/to/image.png") # 加载图片 self.label.setPixmap(self.image) # 在 label 中显示图片 self.rotate_image(30) # 将图片旋转 30 度 def rotate_image(self, angle): center = self.label.geometry().center() # 获取 label 中心点坐标 transform = QTransform().rotate(angle, center) # 构造旋转变换 self.image = self.image.transformed(transform) # 应用变换到图片上 self.label.setPixmap(self.image) # 更新 label 中的图片显示 ``` 在上述示例代码中,我们定义了一个 `MyWindow` 类,其中包含一个 `QLabel` 和一张图片。调用 `rotate_image` 方法可以对图片进行旋转旋转角度由 `angle` 参数指定。在该方法中,我们首先获取 label 的中心点坐标,然后构造一个 `QTransform` 对象并使用 `rotate` 方法对其进行旋转操作。最后,将变换应用到图片上,并更新 label 中的图片显示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值