PDA

View Full Version : Rotation problem



boss_bhat
17th January 2007, 12:22
Hi all,

I am new to Qt, currently I am using Qt 4 on Linux. I am writing a application similar to the "draggableicons" sample. All I need is icons should rotate say 40 degrees when we right click on it. Can some body explain how to do it? Here is the code I have modified
in the draggableicons sample, but it doesnt work :(


void DragWidget::mousePressEvent(QMouseEvent *event)
{
.
.
.
QPixmap tempPixmap = pixmap;
QPainter painter;
painter.begin(&tempPixmap);
//rotate abt 60 degrees
painter.rotate(60);
painter.fillRect(pixmap.rect(), QColor(127, 127, 127, 127));
painter.end();
.
.
.

}

Thanks in advance,
Prasanna Bhat

camel
17th January 2007, 14:37
Do not try to paint outside of the paint event (http://doc.trolltech.com/4.2/qwidget.html#paintEvent).

have a Variable containing the current rotation which you use in the paint event to redraw the screen. When you register a click, update the variable and request an update (http://doc.trolltech.com/4.2/qwidget.html#update).

jpn
17th January 2007, 14:58
boss_bha, the problem is that you copy a pixmap as it is, then create and rotate the painter, and fill a rotated rectangle over the copied pixmap. So the pixmap is not rotated anywhere in that code.


Do not try to paint outside of the paint event (http://doc.trolltech.com/4.2/qwidget.html#paintEvent).
This applies only to QWidgets and he seems to be painting on a QPixmap.

jpn
17th January 2007, 15:13
Here's something to get you going. This might be a bit slow (due to QPixmap->QImage->QPixmap conversions), but close to a working solution:


QImage tempImage = pixmap.toImage();
//rotate abt 60 degrees
QMatrix matrix;
matrix.rotate(60);
tempImage = tempImage.transformed(matrix);
QPainter painter;
painter.begin(&tempImage);
painter.fillRect(tempImage.rect(), QColor(127, 127, 127, 127));
painter.end();

child->setPixmap(QPixmap::fromImage(tempImage));

What does it miss is some proper scaling. A rotated image takes naturally more space to get drawn entirely.
Another way is not to copy the pixmap, but draw it after the painter has been rotated.

boss_bhat
17th January 2007, 15:55
Hi JP-N,

Sorry to bug you again,
I tried your code but it's not working, Am I wrong here?? I am pasting the entire code of mousePressEvent()


void DragWidget::mousePressEvent(QMouseEvent *event)
{
QLabel *child = static_cast<QLabel*>(childAt(event->pos()));
if (!child)
return;

QPixmap pixmap = *child->pixmap();

QByteArray itemData;
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
dataStream << pixmap << QPoint(event->pos() - child->pos());

QMimeData *mimeData = new QMimeData;
mimeData->setData("application/x-dnditemdata", itemData);

QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->setPixmap(pixmap);
drag->setHotSpot(event->pos() - child->pos());


if (event->button()==Qt::RightButton){
QImage tempImage = pixmap.toImage();
//rotate abt 60 degrees
QMatrix matrix;
matrix.rotate(60);
tempImage = tempImage.transformed(matrix);
QPainter painter;
painter.begin(&tempImage);
painter.fillRect(tempImage.rect(),
QColor(127, 127, 127, 127));
painter.end();
child->setPixmap(QPixmap::fromImage(tempImage));
}
else
{
QPixmap tempPixmap = pixmap;
QPainter painter;
painter.begin(&tempPixmap);
painter.fillRect(pixmap.rect(), QColor(127, 127, 127, 127));
painter.end();
child->setPixmap(tempPixmap);

}
if (drag->start(Qt::CopyAction | Qt::MoveAction) == Qt::MoveAction)
child->close();
else {
child->show();
child->setPixmap(pixmap);
}
}

Thanks in advance,
Prasanna Bhat

jpn
17th January 2007, 16:46
Could you elaborate what you intend to do? If you plan to rotate the same image multiple times in a row, I'd suggest you to use some another approach. Like for example keeping the original image around..