PDA

View Full Version : X process 99% after 2 hours using drawPixmap



gab74
17th January 2013, 11:03
Hy all,
i've a simple code to draw a rotating ship on a background image. Every second a receive a message with the new parameters to update the draw,
and in my main method i
call

...
QWidget::update();
drawShip(); }


Problem is that after 2 hours of activity all the system become very slow and i noticed that X process is at 99%
Is there any bug in my code ?


This is my simple code :

void Board::drawShip()
{
int X_Background_PPI;
int Y_Background_PPI;


int PPI_SIZE = 220; // is a square
int CIRCLE_OFFSET = 6; // starting point for Circle PPI

X_Background_PPI = 10 +7;
Y_Background_PPI = 358 +7;


X_Nave_PPI = X_Background_PPI + PPI_SIZE/2;
Y_Nave_PPI = Y_Background_PPI + PPI_SIZE/2 ;


QPen Pen (Qt::yellow, 4, Qt::SolidLine);
QBrush Brush (Qt::black, Qt::SolidPattern);

QPainter painter(this);
QPainter painter1(this);
QPainter painter2(this);
QPainter painter3(this);

painter.setRenderHint(QPainter::Antialiasing, true);
painter1.setRenderHint(QPainter::Antialiasing, true);

painter.drawPixmap(X_Background_PPI,Y_Background_P PI, Background_PPI); // draw the bacground


// draw the ship shape
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, Nave_PPI );
painter1.restore(); // // restore the previous painter settings

int diameter = PPI_SIZE - 2 * CIRCLE_OFFSET ;
int diameter1 = diameter - 80 ;
int diameter3 = diameter - 180 ;

int Wx, Wy = 0;
int Wx1, Wy1 = 0;

float New_Wind_rad;

New_Wind_rad = New_Wind * grad2rad;

Wx = X_Nave_PPI + (diameter/2 * sin(New_Wind_rad) ); // in radiants !!! warning int and float
Wy = Y_Nave_PPI - (diameter/2 * cos(New_Wind_rad) ); // in radiants !!! warning int and float

Wx1 = X_Nave_PPI + (diameter1/2 * sin(New_Wind_rad) ); // in radiants !!! warning int and float
Wy1 = Y_Nave_PPI - (diameter1/2 * cos(New_Wind_rad) ); // in radiants !!! warning int and float


// draw the wind vector
painter1.save(); // save the current printer settings before changing them
painter1.translate(Wx,Wy); // // the point about which the image rotates
painter1.rotate(New_Wind); //degrees;
painter1.drawPixmap(- Wind_Arrow.width()/2,0, Wind_Arrow );
painter1.restore(); // // restore the previous painter settings
}

Santosh Reddy
17th January 2013, 11:31
Take a look at this warning in Qt Docs

Warning: When the paintdevice is a widget, QPainter can only be used inside a paintEvent() function or in a function called by paintEvent(); that is unless the Qt::WA_PaintOutsidePaintEvent widget attribute is set. On Mac OS X and Windows, you can only paint in a paintEvent() function regardless of this attribute's setting.

gab74
18th January 2013, 10:03
Yes, i do not post all the code.. but i call the qpaintevent in my :


void drawShip()
{
int X_Background_PPI;
int Y_Background_PPI;



int PPI_SIZE = 220; // is a square
int CIRCLE_OFFSET = 6; // starting point for Circle PPI

MyFramework:: paintEvent(event);

....


All seems to work well, i see the ship rotating according to the received data, problem is that after 2 hours the system become very slow and i noticed that X process is at 99%

Am i missing something ?? What's wrong with my code ?

Thanks Gabriele

Santosh Reddy
18th January 2013, 10:05
QPainter can only be used inside a paintEvent() function or in a function called by paintEvent();

gab74
18th January 2013, 11:29
OK...sorry for my ignorance...but i have 2 question to better understand :

1) Why my code works, even if i use a simply call to qpaintevent implemented in the framework that i use ?
2) I've to reimplement qpaintevent function in my Widget code ? is it right ?

Thanks in advance for your precious support.

Santosh Reddy
18th January 2013, 11:50
1) Why my code works, even if i use a simply call to qpaintevent implemented in the framework that i use ?
What documentation says is not to use QPainter outside paintEvent(), it does not explicitly not mention why? May be some kind of design constraint, or may be some feature will not work outside paintEvent().

I know it works, I too used it some time back. I was painting a kind of water mark on the all the widgets using event filters. We can never be sure when it will break. So better not use in a way which is explicitly mention not to.


2) I've to reimplement qpaintevent function in my Widget code ? is it right ?
Yes, and simply call drawShip() from PaintEvent.

gab74
24th January 2013, 18:02
Ok so i do this implementation :

in .h i add

void paintEvent(QPaintEvent *event);

in my .cpp i write :

void MyCass :: paintEvent(QPaintEvent *event)
{
drawShip();

}

and i call

QWidget::update(); // to update paint event

when i receive the message to update the ship.
Is this right ??

Thanks Gabriele