PDA

View Full Version : drawRect inside paintGL()



mickey
14th April 2006, 11:36
Hi, I'm trying to draw a rect in myGLWidget; it seems work; but I can see rect while I rotate the scene with mouseMove (and paintGL starts evey time I move mouse). Why this?


//paintGL
painter->setPen(QPen(QColor(255,255,255),1,QPen::DotLine));
painter->drawRect(10,10,50,50);

jacek
14th April 2006, 12:09
You can see the rectangle, so what's the problem?

mickey
14th April 2006, 13:51
No I can see retangle; I can see only while I rotate scene (an paintGl is called); when I stop mouseMoveEvent ( and so paintGL call), rettangle isn't visible; and in console appear
a message


QWidget (myWidget1): deleted while being painted
QPaintDevice: Cannot destroy paint device that is being painted. Be sure to QPainter::end() painters!
Where must I call painter.end()?
thanks

jacek
14th April 2006, 14:10
It's hard to tell just from 2 lines of code. What does your mouseMoveEvent() do?

mickey
14th April 2006, 14:24
translate the scene and call updateGL() for update the scene; the square apperar only when I pressmouse+move = updateGL()

jacek
14th April 2006, 16:06
How do you create and destroy that painter?

mickey
14th April 2006, 16:41
class myWidgetGL {
QPainter* painter;
}
myWidgetGL::myWidgetGL() {
painter = new QPainter(this);
}
I don't destroyed painter; in fact I have that message prevoius posted; I think I need put painter.end() in somewhere ( I don't sure where: maybe in ~myWidgetGL()?)
Thanks

jacek
14th April 2006, 17:12
Better try creating that painter on the stack every time you need it.

mickey
14th April 2006, 22:20
mmm but I need it fixed displayed on myWidgetGL....

jacek
14th April 2006, 22:34
Just create the painter on the stack in the method which uses it, this way it will be destroyed in the right time and you won't have to bother with QPainter::end().

mickey
17th April 2006, 11:02
Sorry, but how do I create painter on the stack?? But the problem of have fixed rect on my QGLWidget persist....
Thanks

jacek
17th April 2006, 13:26
QPainter painter( this );
painter.setPen(QPen(QColor(255,255,255),1,QPen::Do tLine));
painter.drawRect(10,10,50,50);

mickey
17th April 2006, 13:40
sorry but I tried also this (I insert in paintGL() ) but the rect isn't appear; apper only while I traslate/rotate my scene (when updateGL()); maybe is there something that delete it?mmm; (ok for problem of painter.end()). Thanks

jacek
17th April 2006, 14:12
Then try:
void myWidgetGL::paintEvent( QPaintEvent *event )
{
QGLWidget::paintEvent( event );

QPainter painter( this );
painter.setPen(QPen(QColor(255,255,255),1,QPen::Do tLine));
painter.drawRect(10,10,50,50);
}

mickey
17th April 2006, 19:18
with your code console ask me this: 'painter.begin(this);' adding this istruction seems works , but when I traslate /rotating my scene square disappear (calls to updateGL() ); strange: if I click on my scene (updateGL occurs) rect appear; I see that If I moveMouse with buttonMouse down paintEvent() not occurs; instead If I click only, paintEvent occur();
Furthermore I have QWidget::mouseLeave() where inside there's an updateGL(): when I mouse mouse out, updateGL occurs and rect disappear!
mmmmm....

jacek
17th April 2006, 20:01
It looks like QGLWidget repaints are handled in a special way. You might try rendering your scene to a pixmap, painting on that pixmap and then displaying it, but this will affect the performance.

mickey
18th April 2006, 01:12
I understand; updateGL(9 doesn't call paint event; update() insted, yes!
I change this below and now rect is visible (but flickering);


void myMainForm::myUpdateWidgets() {
if (this->MultipleView->isShown()){
myWidget2->update();
myWidget3->update();
..........................................
}
But now arise another problem: the update of widget2 is not fuid! I know updateGL() calls paintGL immediately; update() schedule the call to paintGL() for when widgetGL need an update; I suspect that in my multipleView (I can see wid2 and wid3) widget3 is the last drawn; and paintGL() for widget2 starts later than necessay. Can be the truth?
How can avoid this? Thanks