2

我在 Qt 中旋转图像时遇到了一些问题。每次我用QPainter旋转我的图像时,它都会变得越来越扭曲。这是初始图像:

http://imageshack.us/photo/my-images/838/ship1.png/

经过一些迭代,它变成:

http://imageshack.us/photo/my-images/11/ship2e.png/

这是我的代码:

void Ship::Move(int x, int y)
{
    QPixmap rotatePixmap(shipPixels.size());
    rotatePixmap.fill(Qt::transparent);

    QTransform transform;
    transform.translate(
            rotatePixmap.size().width() / 2,
            rotatePixmap.size().height() / 2
    );

    transform.rotate(degree);
    transform.translate(
            - rotatePixmap.size().width() / 2, 
            - rotatePixmap.size().height() / 2
    );

    QPainter p(&rotatePixmap);
    p.setRenderHints(
            QPainter::Antialiasing | QPainter::SmoothPixmapTransform,
            true
    );

    p.setTransform(transform);
    p.drawPixmap(0, 0, shipPixels);
    p.end();

    shipPixels = rotatePixmap;
    this->setPixmap(shipPixels);
    this->move(QPoint(x, y));
    degree = 0;
}

对我来说,Qt 保留了图像质量。这种行为完全奇怪。有原因吗?

4

2 回答 2

1

根据答案,新代码是这样的。发现自己遇到相同问题的其他用户可以获取此代码:

void Ship::Move(int x, int y)
{
    QPixmap sourceImage(shipPixels);

    QPixmap rotatePixmap(sourceImage.size());
    rotatePixmap.fill(Qt::transparent);

    QTransform transform;
    transform.translate(sourceImage.size().width() / 2, sourceImage.size().height() / 2);
    transform.rotate(degree);
    transform.translate(-sourceImage.size().width() / 2, -sourceImage.size().height() / 2);

    QPainter p;
    p.setRenderHints(QPainter::Antialiasing|QPainter::SmoothPixmapTransform, true);
    p.begin(&rotatePixmap);
    p.setTransform(transform);
    p.drawPixmap(0, 0, sourceImage);
    p.end();

    rotatePixmap.save("temp.png");

    this->setPixmap(rotatePixmap);
    this->move(QPoint(x, y));
}
于 2012-10-11T10:33:12.080 回答
-3
QPainter pixmap;
pixmap.setRenderHints(QPainter::Antialiasing|QPainter::SmoothPixmapTransform,true);

这就是你要找的。

于 2016-10-12T13:28:07.987 回答