image - 无法让图像在Qt中中心旋转

标签 image qt rotation

我正在尝试在 QLabel Widge 内旋转 89x89 图像。

#include "header.h"

disc::disc(QWidget *Parent) : QWidget(Parent)
{
    orig_pixmap = new QPixmap();
    rotate = new QLabel(this);
    showDegrees = new QLabel(this);

    orig_pixmap->load("pic.png");
    degrees = 0;

    rotate->resize(89,89);
    rotate->move(100,10);
    rotate->setStyleSheet("QLabel{background-color:red;}");

    showDegrees->resize(100,100);
    showDegrees->move(400,0);
}

void disc::rotate_disc()
{
    QString string;
    degrees++;
    if(degrees == 360) degrees = 0;

    QTransform rotate_disc;

    rotate_disc.translate( (orig_pixmap->width()-rotate->width())/2 , (orig_pixmap->width()-rotate->height())/2);
    rotate_disc.rotate(degrees,Qt::ZAxis);

    QPixmap pixmap;
    //pixmap = orig_disc->scaled(89,89,Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
    pixmap = orig_pixmap->transformed(rotate_disc,Qt::SmoothTransformation);
    rotate->setPixmap(pixmap);

    string.append("Degrees: " + QString::number(degrees) + "*");
    showDegrees->setText(string);
}

即使它旋转。图像的一半滚动到 QLabel 之外,因此该侧不可见。我怎样才能使其在图像中心的原点(0,0)处中心旋转。

这是文件

http://www65.zippyshare.com/v/85775455/file.html

如果你看它,你会发现图像就像向左弹跳。我怎样才能让它在黑色区域内旋转。

我每 10 毫秒向rotate_disc() 函数设置一个信号超时。我正在用它来深入学习 Qt。

谢谢!

最佳答案

我就是这样做的...

//Move windows's coordinate system to center.
QTransform transform_centerOfWindow( 1, 0, 0, 1, width()/2, height()/2 );

//Rotate coordinate system.
transform_centerOfWindow.rotate( m_LoadingDisk_degrees );

//Draw image with offset to center of image.
QPainter painter( this );

//Transform coordinate system.
painter.setTransform( transform_centerOfWindow );

//Load image.

//Notice: As coordinate system is set at center of window, we have to center the image also... so the minus offset to come to center of image.
painter.drawPixmap( -loadingDisk.width()/2, -loadingDisk.height()/2, loadingDisk );

关于image - 无法让图像在Qt中中心旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10767587/

相关文章:

wpf - 如何将图像 "placemarkers"放在 WPF 中的滚动条内?

android - 方向改变时视频重新开始

ios - GMImagePicker 导致应用程序崩溃

JavaScript - 如何添加 2 个图像的 alpha 以使结果 alpha = 1?

php - 使用 MySQL 和 PHP 无法保存图像名称,而图像名称具有 `+` 符号

c++ - 当我按 alt-tab 键进入其中任何一个窗口时,如何将所有窗口强制置于最前面?

c++ - 如何将 QAction 添加到 QToolBar 中的特定位置

c++ - Qt 不会链接任何东西

c++ - 在 GLM 旋转对象不再居中之后

javascript - 计算 3D 空间中旋转后的顶点位置