如果在不保持纵横比的情况下调整大小,则旋转 QGraphicsPixmapItem 会导致剪切 [英] Rotate QGraphicsPixmapItem results in shear if resizing without keeping aspect ratio

查看:62
本文介绍了如果在不保持纵横比的情况下调整大小,则旋转 QGraphicsPixmapItem 会导致剪切的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试旋转 QGraphicsPixmapItem 子项.对于其他 QGraphicsItem,旋转和缩放工作正常.但是对于 QGraphicsPixmapItem,如果尺寸不保持纵横比,我会得到剪切而不是旋转.

I am trying to rotate a QGraphicsPixmapItem child. For other QGraphicsItem, rotation and scaling work fine. But for a QGraphicsPixmapItem, if the size does not keep aspect ratio, instead of rotation I get shear.

示例代码:

#include <QApplication>
#include <QGraphicsView>
#include <QMessageBox>
#include <QGraphicsPixmapItem>
#include <QFileDialog>

int main(int argc, char *argv[])
{
    QGraphicsScene s;
    s.setSceneRect(-200, -200, 500, 500);
    QGraphicsView view(&s);
    view.show();

    QGraphicsPixmapItem p;
    QString fileName = QFileDialog::getOpenFileName(0, QObject::tr("Open Image File"), QString(), QObject::tr("Png files (*.png);;Jpeg files (*.jpg *.jpeg);;Bitmap files (*.bmp)"));
    p.setPixmap(QPixmap(fileName));
    s.addItem(&p);
    QMessageBox::information(0, "", "");

    QTransform original = p.transform();

    // scale aspect ratio then rotate
    QTransform scalingTransform0(0.5, 0, 0, 0, 0.5, 0, 0, 0, 1);
    // p.setTransformOriginPoint(p.boundingRect().center()); // doesn't help shear
    p.setTransform(scalingTransform0 * original);
    p.setRotation(20);
    QMessageBox::information(0, "", "");

    // scale
    QTransform scalingTransform(0.5, 0, 0, 0, 1, 0, 0, 0, 1);
    p.setTransform(scalingTransform * original);
    QMessageBox::information(0, "", "");

    // rotate
    p.setRotation(20);
    QMessageBox::information(0, "", "");

    // unrotate then rotate again
    p.setRotation(0);
    QMessageBox::information(0, "", "");
    QTransform rotTransform = p.transform().rotate(20);
    p.setTransform(rotTransform);

    // or p.rotate(20);
    return app.exec();
}

结果:

对于QGraphicsPixmapItems,我不知道如何获得简单的旋转,没有剪切,并且项目必须记住旋转.

I don't know how to get simple rotation, without shearing, for QGraphicsPixmapItems, and the item must remember the rotation.

推荐答案

为什么 QGraphicsPixmapItem 表现如此不一致仍然是个谜.

It is still a mystery why QGraphicsPixmapItem behaves so inconsistently.

解决办法:
缩放项目时,缩放像素图,将生成的像素图应用于项目.
在这种情况下,旋转将起作用(因为 QGraphicsPixmapItem 并没有真正缩放).

A solution:
When scaling the item, scale the pixmap, apply resulting pixmap to item.
In that case rotation will work (because QGraphicsPixmapItem is not really scaled).

QPixmap p1 = pixmap.scaled(100, 100, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
p.setPixmap(p1);
p.setRotation(20);

这种方法会降低质量,所以我最终重新加载了文件

This approach loses quality, so I ended up reloading the file

QPixmap p1 = (QPixmap(fileName)).scaled(100, 100, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
p.setPixmap(p1);
p.setRotation(20);

如果有更好的解决方案,我很乐意看到.

If there is a better solution, I will be happy to see it.

这篇关于如果在不保持纵横比的情况下调整大小,则旋转 QGraphicsPixmapItem 会导致剪切的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆