Results 1 to 17 of 17

Thread: drawRect inside paintGL()

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default drawRect inside paintGL()

    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?
    Qt Code:
    1. //paintGL
    2. painter->setPen(QPen(QColor(255,255,255),1,QPen::DotLine));
    3. painter->drawRect(10,10,50,50);
    To copy to clipboard, switch view to plain text mode 
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drawRect inside paintGL()

    You can see the rectangle, so what's the problem?

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: drawRect inside paintGL()

    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
    Qt Code:
    1. QWidget (myWidget1): deleted while being painted
    2. QPaintDevice: Cannot destroy paint device that is being painted. Be sure to QPainter::end() painters!
    To copy to clipboard, switch view to plain text mode 
    Where must I call painter.end()?
    thanks
    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drawRect inside paintGL()

    It's hard to tell just from 2 lines of code. What does your mouseMoveEvent() do?

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: drawRect inside paintGL()

    translate the scene and call updateGL() for update the scene; the square apperar only when I pressmouse+move = updateGL()
    Regards

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drawRect inside paintGL()

    How do you create and destroy that painter?

  7. #7
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: drawRect inside paintGL()

    Qt Code:
    1. class myWidgetGL {
    2. QPainter* painter;
    3. }
    4. myWidgetGL::myWidgetGL() {
    5. painter = new QPainter(this);
    6. }
    To copy to clipboard, switch view to plain text mode 
    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
    Regards

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drawRect inside paintGL()

    Better try creating that painter on the stack every time you need it.

  9. #9
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: drawRect inside paintGL()

    mmm but I need it fixed displayed on myWidgetGL....
    Regards

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drawRect inside paintGL()

    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().

  11. #11
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: drawRect inside paintGL()

    Sorry, but how do I create painter on the stack?? But the problem of have fixed rect on my QGLWidget persist....
    Thanks
    Regards

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drawRect inside paintGL()

    Qt Code:
    1. QPainter painter( this );
    2. painter.setPen(QPen(QColor(255,255,255),1,QPen::DotLine));
    3. painter.drawRect(10,10,50,50);
    To copy to clipboard, switch view to plain text mode 

  13. #13
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: drawRect inside paintGL()

    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
    Regards

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drawRect inside paintGL()

    Then try:
    Qt Code:
    1. void myWidgetGL::paintEvent( QPaintEvent *event )
    2. {
    3. QGLWidget::paintEvent( event );
    4.  
    5. QPainter painter( this );
    6. painter.setPen(QPen(QColor(255,255,255),1,QPen::DotLine));
    7. painter.drawRect(10,10,50,50);
    8. }
    To copy to clipboard, switch view to plain text mode 

  15. #15
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: drawRect inside paintGL()

    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....
    Regards

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drawRect inside paintGL()

    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.

  17. #17
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: drawRect inside paintGL()

    I understand; updateGL(9 doesn't call paint event; update() insted, yes!
    I change this below and now rect is visible (but flickering);
    Qt Code:
    1. void myMainForm::myUpdateWidgets() {
    2. if (this->MultipleView->isShown()){
    3. myWidget2->update();
    4. myWidget3->update();
    5. ..........................................
    6. }
    To copy to clipboard, switch view to plain text mode 
    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
    Regards

Similar Threads

  1. paintGL() and initializeGL() help
    By mickey in forum Newbie
    Replies: 14
    Last Post: 26th February 2006, 18:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.