PDA

View Full Version : drawPixmap - image disappears



gab74
6th March 2013, 16:54
Good morning,
i've to draw an arrow on a widget. so i have the xpm file for the arrow.
I draw the arrow only if the new angle is more than 5 degree.
Problem is that on my widget i see the arrow, then it disappears and come back only when the drawship metod is call again, then disappears again..and so on..as flickering...

I thought that when drawpxmap draws the image it remains on the widget untill a new drawpixmap will be called...

What is my error ?

This is the simple code :



void Class:: paintEvent(QPaintEvent *event)
{


if(fabs(New_Heading - OSHeading) > 5 )
{

drawShip();
}
}


void Class::drawShip()
{


QRegion myregion1(X_Background_PPI, Y_Background_PPI, PPI_SIZE, PPI_SIZE);

QPainter painter1(this);
painter1.setClipRegion(myregion1);
painter1.setRenderHint(QPainter::Antialiasing, true);

painter1.save(); // save the current printer settings before changing them
painter1.translate(X_Nave_PPI,Y_Nave_PPI); // // the point about which the image rotates
painter1.rotate(New_Heading); //degrees;
painter1.drawPixmap(-Nave_PPI.width()/2, - Nave_PPI.height()/2, ARROW_PPI );
painter1.restore(); // // restore the previous painter settings
}

lanz
7th March 2013, 05:42
I thought that when drawpxmap draws the image it remains on the widget untill a new drawpixmap will be called...
No, every time the widget is repainted (it can happen for a number of reasons like resizing, minimizing/restoring window, etc...) it is cleared of all content and paintEvent called.
So you need to draw all widget contents each time paintEvent occurs.
In your case you draw it once, then when another paintEvent is fired, your code takes else branch and draws nothing, so the widget stays empty.
If you concerned about performance (which is maybe prematurely optimization in your case) you can draw your scene to backbuffer pixmap and then draw only that pixmap in paintEvent.
Also you can always switch to OpenGL.

gab74
7th March 2013, 08:32
OK thankyou !!
One question :

You say i've to copy my pixmap to a backbuffer pixmap...
PLease can you tell me how do this ?
Consider that in my drawship method i have 2
drawpixmap call: one to draw an arrow and another to draw a ship.

painter1.drawPixmap(-Nave_PPI.width()/2, - Nave_PPI.height()/2, ARROW_PPI );
painter1.drawPixmap(-Nave_PPI.width()/2, - Nave_PPI.height()/2, SHIP_PPI );

How can i create a backbuffer pixmap containing the pixmap generated by the 2 commands ?

Thanks Gabriele

lanz
11th March 2013, 06:13
You can use pixmap as paint device:

class Class {
QPixmap *backbuffer;
};
void Class::drawShip () {
QPainter back_painter (this->backbuffer);
// drawing code
};
void Class::paintEvent(QPaintEvent *event) {
QPainter painter1(this);
painter1.drawPixmap (0,0, this->backbuffer);
};

Of course you need to initialize pixmap, and keep it at actual size(e.g. after resize events) and valid state (after ship rotated or moved).