PDA

View Full Version : Paint event function in key press event



soumya
15th January 2010, 10:26
Hi All,

How to call a paint event function in key press event can anybody help me with the code snippet.



Regards,
soumya

yogeshgokul
15th January 2010, 10:47
You just need to call repaint() or update() function within keyPressEven().
Qt suggests to call update() to avoid slowdown and flickering.

faldzip
15th January 2010, 10:50
Call update like this:


update();


EDIT:
Oops... i was late :P

soumya
15th January 2010, 11:09
Hi thanks for the reply i tried with update function but i am getting error as ,

circleform.cpp: In function ‘void update()’:
circleform.cpp:423: error: invalid use of ‘this’ in non-member function

and here is the code snippet

void circleForm::keyPressEvent( QKeyEvent *event )
{

switch ( event->key() )
{
case Qt::Ke_Enter:
update();
break;
}
}

void update(){
QPainter painter(this); // error
painter.setPen(QPen(Qt::black, 3,Qt::SolidLine));
painter.drawEllipse(100, 140, 160 , 160);
}

Regards,
Soumya

yogeshgokul
15th January 2010, 11:13
Hi thanks for the reply i tried with update function but i am getting error as ,

circleform.cpp: In function ‘void update()’:
circleform.cpp:423: error: invalid use of ‘this’ in non-member function

and here is the code snippet

void circleForm::keyPressEvent( QKeyEvent *event )
{

switch ( event->key() )
{
case Qt::Ke_Enter:
update();
break;
}
}

void update(){
QPainter painter(this); // error
painter.setPen(QPen(Qt::black, 3,Qt::SolidLine));
painter.drawEllipse(100, 140, 160 , 160);
}

Regards,
Soumya


You never need to override update function. You can do all kind of drawing in paintEvent(). And calling update() function will call paintEvent() automatically.
So move your code from update() to paintEvent().
And then call update() from keyPressEvent().

soumya
2nd February 2010, 13:13
Hi,

I need to draw a circle on the frame which is at the top left corner as shown in the attachment. User can enter certain inputs
and when the user press the enter key the circle should be drawn on the frame any suggestions

aamer4yu
2nd February 2010, 13:40
Inherit the frame and draw using QPainter