2

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

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.

Here is the file

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.

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

Thank You!

wenn32
  • 1,256
  • 2
  • 17
  • 26
  • Please do not link to files on a one-click-hoster, even less an executable file. – leemes May 26 '12 at 16:02
  • yeah i was on mobile.i copied the zip file into my mobile.so i couldn't open any non one-click-hoster cause most of them don't open in my mobile or DISPLAY properly :-(.Sorry for the trouble – wenn32 May 27 '12 at 04:25
  • Try looking at this: http://stackoverflow.com/questions/4665606/rotate-image-in-qt – Blastcore May 27 '12 at 16:36
  • as per the doc, QMatrix should be avoided.anyways i found that it can be done using mathematical formula. – wenn32 May 28 '12 at 16:59

3 Answers3

3

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 );
Dusda
  • 3,347
  • 5
  • 37
  • 58
1

I think all you're really missing is the initial translation to do the rotation around the centre of the pixmap.

Move it to the origin, rotate, then move it back. And remember, transformations are applied in reverse order from how you'd expect, given how you specify them in the code.

QTransform rotate_disc;
rotate_disc.translate(orig_pixmap->width()/2.0 , orig_pixmap->height()/2.0);
rotate_disc.rotate(degrees);
rotate_disc.translate(-orig_pixmap->width()/2.0 , -orig_pixmap->height()/2.0);
cgmb
  • 4,284
  • 3
  • 33
  • 60
0

If you are making a loading indicator, animated gif would be much easier. See GIF animation in Qt

Community
  • 1
  • 1
  • 1
    NO i just want to make it rotate as i said i am just using this to learn that's it.i know how to use GIF animation in Qt just want to make a plane pic rotate perfectly But thanks for the reply :-) – wenn32 May 27 '12 at 04:21