Results 1 to 3 of 3

Thread: qglwidget overpaint

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default qglwidget overpaint

    Hi there,

    I would like to achieve what is being done in the overpaint example of QGLViewer: render a 3D scene with OpenGL and paint something *on top* of that with a QPainter. I followed the instructions on that website and did this:

    Qt Code:
    1. class OpenGLWidget: public QGLViewer
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. OpenGLWidget(QWidget* parent=0);
    7. ~OpenGLWidget();
    8.  
    9. protected:
    10. void paintEvent(QPaintEvent *event); // override paintEvent
    11.  
    12. };
    13.  
    14. OpenGLWidget::OpenGLWidget(QWidget *parent) : QGLViewer(parent)
    15. {
    16. setAttribute(Qt::WA_NoSystemBackground);
    17. }
    18.  
    19. void OpenGLWidget::paintEvent(QPaintEvent *event)
    20. {
    21. QGLViewer::paintEvent(event); // Do what the overridden method would do.
    22.  
    23. // Paint something with QPainter.
    24. QPainter painter;
    25. painter.setBackgroundMode(Qt::TransparentMode);
    26. painter.begin(this); // This is where it happens.
    27. painter.setPen(colorUtil.penBlueThick);
    28. painter.drawLine(0, 0, 100, 100);
    29. }
    To copy to clipboard, switch view to plain text mode 

    Unfortunately this does not have the desired effect. When I begin() the painter, the background is cleared and the 3D rendering disappears. Setting the background mode to transparent, and setting the widget attribute to NoSystemBackground have no effect. Can anyone help me along please?

    Thanks
    Cruz

  2. #2
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qglwidget overpaint

    Good, by now I managed to make it work like this:

    Qt Code:
    1. void OpenGLWidget::paintEvent(QPaintEvent *event)
    2. {
    3. QPainter painter;
    4. painter.begin(this);
    5. painter.setRenderHint(QPainter::Antialiasing);
    6.  
    7. // Save current OpenGL state
    8. glPushAttrib(GL_ALL_ATTRIB_BITS);
    9. glMatrixMode(GL_PROJECTION);
    10. glPushMatrix();
    11. glMatrixMode(GL_MODELVIEW);
    12. glPushMatrix();
    13.  
    14. // Setup OpenGL context.
    15. setBackgroundColor(QColor(255,255,255));
    16. setForegroundColor(QColor(0,0,0));
    17. setFont(QFont("Helvetica", 18));
    18. glEnable(GL_DEPTH_TEST);
    19. glDisable(GL_LIGHTING);
    20. glEnable(GL_BLEND);
    21. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    22.  
    23. drawOpenGL(); // draw a bunch of opengl things
    24.  
    25. // Restore OpenGL state
    26. glMatrixMode(GL_MODELVIEW);
    27. glPopMatrix();
    28. glMatrixMode(GL_PROJECTION);
    29. glPopMatrix();
    30. glPopAttrib();
    31.  
    32. drawOverPaint(&painter); // 2D QPainter drawing
    33.  
    34. painter.end();
    35. }
    To copy to clipboard, switch view to plain text mode 

    What I don't understand is, on one computer this works perfectly and the QPainter code is drawn over the OpenGL, on another computer the OpenGL is on top of the QPainter drawing and covers it fully.
    How do I have control over what gets painted over what?

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: qglwidget overpaint

    QGLViewer is derived from QOpenGLWidget. See the docs for that class on how to combine QPainter and OpenGL in a single paint event:

    It is also possible to draw 2D graphics onto a QOpenGLWidget subclass using QPainter:

    - In paintGL(), instead of issuing OpenGL commands, construct a QPainter object for use on the widget.
    - Draw primitives using QPainter's member functions.
    - Direct OpenGL commands can still be issued. However, you must make sure these are enclosed by a call to the painter's beginNativePainting() and endNativePainting().
    The order in which you do this would likely affect which graphics are "on top". You may not have to do all of the state saving / restoring.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. The following user says thank you to d_stranz for this useful post:

    Cruz (16th May 2019)

Similar Threads

  1. One QGLWidget that manage QGLWidget childs
    By polch in forum Qt Programming
    Replies: 4
    Last Post: 12th December 2012, 12:26
  2. Using more than one QGLWidget?
    By Paulpro in forum Qt Programming
    Replies: 0
    Last Post: 1st November 2010, 08:43
  3. QGLWidget and VBO
    By kaszewczyk in forum Newbie
    Replies: 1
    Last Post: 6th May 2010, 11:04
  4. QGLWidget
    By manmohan in forum Newbie
    Replies: 2
    Last Post: 5th June 2009, 13:54
  5. QGLWidget bug
    By Wizard in forum Qt Programming
    Replies: 11
    Last Post: 31st August 2007, 12:23

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.