can't get the image to rotate in center in Qt

给你一囗甜甜゛ 提交于 2019-12-24 00:56:12

问题


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!


回答1:


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



回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/10767587/cant-get-the-image-to-rotate-in-center-in-qt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!