qt 加载 图片旋转_无法使图像在Qt的中心旋转

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!

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值