PDA

View Full Version : paintEvent problem



MoaLaiSkirulais
12th February 2008, 12:39
look at this code, it only draw ONE ellipse.
I want to draw TWO ellipses.
What is wrong here?? :mad::mad:

shape.cpp


void shape::paintEvent( QPaintEvent * )
{
QPainter painter( this );

QRectF rectangle1(10.0, 20.0, 80.0, 60.0);
painter.drawEllipse(rectangle1);

QRectF rectangle2(100.0, 200.0, 80.0, 60.0);
painter.drawEllipse(rectangle2);

}

void shape::drawMyCircle()
{

QPainter painter( this );
QRectF rectangle(150.0, 250.0, 80.0, 60.0);
painter.drawEllipse(rectangle);

}


main.cpp


#include "shape.h"
#include <QApplication>


int main( int argc, char **argv )
{

QApplication a ( argc, argv );

shape myShape;
myShape.resize(640, 480);
myShape.show();
myShape.drawMyCircle ();
myShape.show(); //<--- why dont refresh?

return a.exec ();

}

marcel
12th February 2008, 13:01
What is wrong here?? :mad::mad:

Almost everything!

You can't paint on a widget from outside its paint event. So the method drawMyCircle doesn't do do anything.

Remove the call to drawMyCircle from main.cpp and also remove the second call to show.
You will then see two ellipses(the ones from paintEvent), provided that you don't draw off-widget.

MoaLaiSkirulais
12th February 2008, 13:09
thnx!! :)

so if only the widget :: painEvent() can effective draw, how can i encapsulate the draw definition of any entity that is not a widget?? for example "myShape()"...

each entitie must to be a widget??

i desire each entity draw itself.... :eek:

marcel
12th February 2008, 13:17
In that case maybe QGraphicsView Framework (http://doc.trolltech.com/4.3/graphicsview.html) is more suitable for you.
You can create custom QGraphicsItem's to draw your custom shapes, although basic shapes as rectangle and ellipse are already covered by the framework.

MoaLaiSkirulais
12th February 2008, 13:45
thnx...

btw suppose i still want to use QPainter's and QWidgets schema....

is it possible from QWidget :: paintEvent() to trigger using SIGNALs and SLOTs each myCustomPaintEvent of each entity?? ex. myShape :: myCustomPaintEvent() ???

:confused:

marcel
12th February 2008, 13:59
You can create one widget subclass for each shape you want to draw.
This is the easiest way if you want to use widgets for this.

Or you can create a single QWidget subclass with methods that draw all the shapes you need, taking as parameter a QPainter pointer. Also you have to add an enum specifying the shape type.
In the paint event of this widget you can test the shape type and call the right method.