全网最全!!Qt实现图片旋转及图片旋转动画的几种方式

实现图片旋转的两种方式

第一种方案

使用 QPixmap 的 transformed 函数来实现旋转,这个函数默认是以图片中心为旋转点,不能设置旋转的中心点,使用如下:

QMatrix matrix;
matrix.rotate(45);

QLabel *Label= new QLabel();
Label->setPixmap(QPixmap(“:/images.png”)
        .transformed(matrix, Qt::SmoothTransformation));

第二种方案

使用 QPainter 这位“画家”,示例程序如下:

void Widget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap disc(":/disc.png");

    /* 设定旋转中心点 */
    painter.translate(130,150);
    /* 旋转的角度 */
    painter.rotate(45);
    /* 恢复中心点 */
    painter.translate(-130,-150);
    /* 画图操作 */
    painter.drawPixmap(40,60,180,180, disc);
}

 实现图片旋转动画的两种方式:

 一、借助QPixmap实现旋转动画


    QMatrix matrix;
    matrix.rotate(angle);
    if(angle++ ==360)
        angle = 0;
//设定图片的大小;
    QImage Image = QImage(":/images/cd.png");
    QPixmap pixmap = QPixmap::fromImage(Image);
    QPixmap fixpixmap = pixmap.scaled(320,320,Qt::IgnoreAspectRatio
                            ,Qt::SmoothTransformation);

    QLabel *Label= new QLabel();
    label[1]->setPixmap((fixpixmap)
                .transformed(matrix,Qt::SmoothTransformation));
    label[1]->setAlignment(Qt::AlignCenter);

第二种方案

使用 QPainter 这位“画家”,示例程序如下:

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    disc = QPixmap(":/cd.png");
    timer = new QTimer();
    timer->start(10);
    connect(timer,SIGNAL(timeout()),this,SLOT(timerTimeOut()));
}

void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    /* 碟机转动 */
    if(angle++ == 360)
        angle = 0;
    /* 设定旋转中心点 */
    painter.translate(disc.width()/2,disc.height()/2);
    /* 旋转的角度 */
    painter.rotate(angle);
    /* 恢复中心点 */
    painter.translate(-disc.width()/2,-disc.height()/2);
    /* 画图操作 */
    painter.drawPixmap(0,0,disc.width(),disc.height(), disc);
}

void MainWindow::timerTimeOut()
{
    /* 当界面初始化或者需要刷新时才会执行paintEvent */
    update();
}

使旋转动画始终处于窗口中心:

void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing
                           | QPainter::SmoothPixmapTransform);
    /* 碟机转动 */
    if(angle++ == 360)
        angle = 0;
//设定旋转中心点
    /* QRectF 即,继承 QRect(Qt 的矩形类), F 代表精确到浮点类型 */
    QRect rect((this-> width() - disc.width()) / 2,
               (this-> height() - disc.height()) / 2,
               disc.width(),
               disc.height());
    /* 默认参考点为左上角原点(0,0),因为旋转需要以图形的中心为参考点,
            * 我们使用 translate 把参考点设置为 CD 图形的中心点坐标 */
    painter.translate(0 + rect.x() + rect.width() / 2,
                      0 + rect.y() + rect.height() / 2);
    /* 旋转的角度 */
    painter.rotate(angle);
//恢复中心点;
    /* 现在参考点为 CD 图形的中心,我们需要把它设置回原点的位置,
            * 所以需要减去上面加上的数 即将绘图的起点设置回起点*/
    painter.translate(0 - (rect.x() + rect.width() / 2),
                      0 - (rect.y() + rect.height() / 2));
//画图操作
    /* 画图,QPainter 提供了许多 drawX 的方法 */
    painter.drawPixmap(rect,disc);
}

最终效果: 

 

 

参考:QT 实现图片旋转的两种方法 - 走看看 

  • 13
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Qt实现图片的立体旋转,可以使用QGraphicsPixmapItem类来实现。具体步骤如下: 1. 创建一个QGraphicsScene对象和一个QGraphicsView对象,并将它们关联起来。 2. 加载图片并创建一个QGraphicsPixmapItem对象。 3. 设置QGraphicsPixmapItem对象的旋转中心和旋转角度。 4. 将QGraphicsPixmapItem对象添加到QGraphicsScene对象中。 5. 将QGraphicsScene对象设置为QGraphicsView对象的场景,并显示QGraphicsView对象。 下面是一个简单的示例代码,实现了图片的立体旋转: ```cpp #include <QtWidgets> int main(int argc, char *argv[]) { QApplication app(argc, argv); // 创建场景和视图 QGraphicsScene scene; QGraphicsView view(&scene); // 加载图片 QPixmap pixmap(":/images/image.jpg"); // 创建图片项 QGraphicsPixmapItem pixmapItem(pixmap); // 设置图片项的中心点和旋转角度 pixmapItem.setTransformOriginPoint(pixmap.width() / 2, pixmap.height() / 2); pixmapItem.setRotation(45.0); // 将图片项添加到场景中 scene.addItem(&pixmapItem); // 显示视图 view.show(); return app.exec(); } ``` 在上面的代码中,我们加载了一张图片,并在其中创建了一个QGraphicsPixmapItem对象。然后,我们设置了该对象的中心点和旋转角度,并将该对象添加到了场景中。最后,我们将场景设置为视图的场景,并显示视图。 当我们运行这个程序时,就可以看到图片在场景中立体旋转的效果。 注意:如果需要实现更加复杂的立体旋转效果,可以考虑使用QGraphicsRotation类和QGraphicsTransform类。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值