PDA

View Full Version : Qt3-QPainter rotate exception



Raccoon29
4th September 2007, 16:31
Hi everyone,

I know this is a nooby problem, and however I'm a Qt noob...
My application has a window that loads an image and stores it in a QPixmapLabel.
Then there is a button that pressed should rotate the loaded image of 27 degrees.
The problem is that the first time it works perfectly, the second push (doing nothing else
in the meanwhile) shows an exception, and crushes the program. :confused:
Here I report the routine that fails:



QPicture pic;
QPainter paint;
QPixmap *pix=pxldest->pixmap();

if(paint.begin(&pic))
{
paint.rotate(27);
paint.drawPixmap(0,0,*pix);
paint.end();
pxldest->setPicture(pic);
QMessageBox::information(this,"Rotated","Image rotated");
}

"pxldest" is the QPixmapLabel's name. Note that the exception is thrown by the line 8 during the second rotation.

I would appreciate any sort of tip or warn about this code
and thank you in advance to all will help!

marcel
4th September 2007, 16:35
Because the second time the label's pixmap is null since you set a QPicture instead.
You shouldn't use setPicture, but setPixmap and instead of a QPicture use a QPixmap.

So:


QPainter paint;
QPixmap *pix=pxldest->pixmap();

if(pix)
{
QPixmap rotated(pix->size().width(), pix->size().height());
paint.rotate(27);
paint.drawPixmap(0,0,*pix);
paint.end();
pxldest->setPixmap(rotated);
QMessageBox::information(this,"Rotated","Image rotated");
}


Regards

Raccoon29
4th September 2007, 16:41
...ohhhh :crying: but sure!!!
Well, I'm right a noob...
Thank you very very much marcel ! :D

Raccoon29
4th September 2007, 17:09
Thank you marcel, but just a little modification to make it work for who watch this thread:

QPainter paint;
QPixmap *pix=pxldest->pixmap();
if(pix)
{
QPixmap rotated(pix->size().width(), pix->size().height());
paint.begin(&rotated);
paint.rotate(27);
paint.drawPixmap(0,0,*pix);
paint.end();
pxldest->setPixmap(rotated);
QMessageBox::information(this,"Rotated","Image rotated");
}
you just forgot the "begin" to make it be handled, did you? :)

Greetings

marcel
4th September 2007, 17:43
Yes, I removed from the condition and forgot to add it back.
Well, that's what happens when you write code inline, without testing it :).

Regards