无法使图像在Qt的中心旋转 [英] can't get the image to rotate in center in Qt

查看:319
本文介绍了无法使图像在Qt的中心旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i am trying to rotate a 89x89 image inside the QLabel Widge.

#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)处旋转.

Even though it rotates. The image's half gets rolled outside the QLabel and hence that side is not visible.How can i make it center rotate at origin(0,0) of the image center.

这是文件

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

如果查看它,您会看到图像就像向左跳动.我如何使其在黑色区域内旋转.

if you look at it you can see that image is like bouncing to the left.how can i make it rotate inside the black area.

我每10ms设置一个Rotate_disc()函数的信号超时.我正在用它来深入学习Qt.

i setup a signal timeout at every 10ms to the rotate_disc() function. I am using this to learn Qt indepth.

谢谢!

推荐答案

我已经这样做了...

I've done it like this...

//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 );

这篇关于无法使图像在Qt的中心旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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