我正在尝试在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
如果你看一下,你可以看到图像就像向左边弹跳一样。我可以让它在黑色区域内旋转。
我将每10ms的信号超时设置为rotate_disc()函数。我正在用它来学习Qt深入研究。
谢谢!
答案 0 :(得分:3)
我这样做了......
//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 );
答案 1 :(得分:1)
我认为你真正缺少的是围绕像素图中心旋转的初始平移。
将其移至原点,旋转,然后向后移动。请记住,根据您在代码中指定转换的方式,转换按照您期望的方式以相反的顺序应用。
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);
答案 2 :(得分:0)
如果您正在制作加载指示器,动画gif会更容易。见GIF animation in Qt