brianc
16th August 2010, 22:25
Can someone please help me figure out how setTransformationOriginPoint works?
I have a simple program where I am trying to rotate an image using the image edges as the rotation axis. If you press the up arrow, it should rotate along the top axis. The bottom arrow should rotate along the bottom axis, etc.
Of course, that's not what happens. For each case, it appears that the rotation axis is 0,0. Can someone please show my what I'm doing wrong, or let me know if there is a better way to rotate images along different axis.
#include "mainwindow.h"
#include <QGraphicsScene>
#include <QHBoxLayout>
#include <QGraphicsView>
#include <QKeyEvent>
MainWindow::MainWindow(QWidget *parent)
: QWidget(parent)
{
QGraphicsScene *scene = new QGraphicsScene();
scene->setSceneRect(-150, -150, 300, 300);
scene->setBackgroundBrush(Qt::white);
item = new QGraphicsPixmapItem(QPixmap(":/images/0.png"));
item->setPos(QPointF(-item->boundingRect().width()/2,-item->boundingRect().height()/2));
scene->addItem(item);
QGraphicsView *view = new QGraphicsView();
view->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
view->setScene(scene);
view->setSceneRect(-150, -150, 300, 300);
view->setFocus();
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(view);
setLayout(layout);
rotateTimeLine = new QTimeLine(1500, this);
rotateTimeLine->setCurveShape(QTimeLine::LinearCurve);
connect(rotateTimeLine, SIGNAL(valueChanged(qreal)),
this, SLOT(updateRotate(qreal)));
}
MainWindow::~MainWindow()
{
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
qDebug("MainWindow::keyPressEvent");
switch (event->key()) {
case Qt::Key_Up:
rotate(0);
break;
case Qt::Key_Right:
rotate(1);
break;
case Qt::Key_Left:
rotate(2);
break;
case Qt::Key_Down:
rotate(3);
break;
}
}
void MainWindow::rotate(int axis)
{
rotateAxis = axis;
rotateAngle = 0;
rotateTimeLine->start();
}
void MainWindow::updateRotate(qreal value)
{
QTransform txf = QTransform();
rotateAngle = 360*value;
switch (rotateAxis)
{
case 0:
// Up arrow - Rotate along top axis
txf.rotate(rotateAngle, Qt::XAxis);
item->setTransformOriginPoint(item->boundingRect().width()/2, item->boundingRect().height()/2);
break;
case 1:
// Right arrow - Rotate along rigth axis
txf.rotate(rotateAngle, Qt::YAxis);
item->setTransformOriginPoint(item->boundingRect().width(), item->boundingRect().height()/2);
break;
case 2:
// Left arrow - Rotate along left axis
txf.rotate(rotateAngle, Qt::YAxis);
item->setTransformOriginPoint(0, item->boundingRect().height()/2);
break;
case 3:
// Down arrow - Rotate along bottom axis
txf.rotate(rotateAngle, Qt::XAxis);
item->setTransformOriginPoint(item->boundingRect().width()/2,item->boundingRect().height());
break;
}
item->setTransform(txf, false);
update();
}
I have a simple program where I am trying to rotate an image using the image edges as the rotation axis. If you press the up arrow, it should rotate along the top axis. The bottom arrow should rotate along the bottom axis, etc.
Of course, that's not what happens. For each case, it appears that the rotation axis is 0,0. Can someone please show my what I'm doing wrong, or let me know if there is a better way to rotate images along different axis.
#include "mainwindow.h"
#include <QGraphicsScene>
#include <QHBoxLayout>
#include <QGraphicsView>
#include <QKeyEvent>
MainWindow::MainWindow(QWidget *parent)
: QWidget(parent)
{
QGraphicsScene *scene = new QGraphicsScene();
scene->setSceneRect(-150, -150, 300, 300);
scene->setBackgroundBrush(Qt::white);
item = new QGraphicsPixmapItem(QPixmap(":/images/0.png"));
item->setPos(QPointF(-item->boundingRect().width()/2,-item->boundingRect().height()/2));
scene->addItem(item);
QGraphicsView *view = new QGraphicsView();
view->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
view->setScene(scene);
view->setSceneRect(-150, -150, 300, 300);
view->setFocus();
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(view);
setLayout(layout);
rotateTimeLine = new QTimeLine(1500, this);
rotateTimeLine->setCurveShape(QTimeLine::LinearCurve);
connect(rotateTimeLine, SIGNAL(valueChanged(qreal)),
this, SLOT(updateRotate(qreal)));
}
MainWindow::~MainWindow()
{
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
qDebug("MainWindow::keyPressEvent");
switch (event->key()) {
case Qt::Key_Up:
rotate(0);
break;
case Qt::Key_Right:
rotate(1);
break;
case Qt::Key_Left:
rotate(2);
break;
case Qt::Key_Down:
rotate(3);
break;
}
}
void MainWindow::rotate(int axis)
{
rotateAxis = axis;
rotateAngle = 0;
rotateTimeLine->start();
}
void MainWindow::updateRotate(qreal value)
{
QTransform txf = QTransform();
rotateAngle = 360*value;
switch (rotateAxis)
{
case 0:
// Up arrow - Rotate along top axis
txf.rotate(rotateAngle, Qt::XAxis);
item->setTransformOriginPoint(item->boundingRect().width()/2, item->boundingRect().height()/2);
break;
case 1:
// Right arrow - Rotate along rigth axis
txf.rotate(rotateAngle, Qt::YAxis);
item->setTransformOriginPoint(item->boundingRect().width(), item->boundingRect().height()/2);
break;
case 2:
// Left arrow - Rotate along left axis
txf.rotate(rotateAngle, Qt::YAxis);
item->setTransformOriginPoint(0, item->boundingRect().height()/2);
break;
case 3:
// Down arrow - Rotate along bottom axis
txf.rotate(rotateAngle, Qt::XAxis);
item->setTransformOriginPoint(item->boundingRect().width()/2,item->boundingRect().height());
break;
}
item->setTransform(txf, false);
update();
}