Hi,

I am trying to add openGL into my program. What I want is to have a gradient background on the back (using QPainter) and say a triangle (openGL) in the front. My problem is that the 2D painting always lies on top of the OpenGL painting. Could anyone show me how I can fix this?

I included the code below in case anyone want to have a look. So what I currently have is only the gradient background and no triangles since they are hidden behind the background.

Thanks a tons


Qt Code:
  1. void GLWidget::paintEvent(QPaintEvent *event)
  2. {
  3. QPainter painter(this);
  4. painter.setPen(Qt::NoPen);
  5. painter.setBrush(gradient);
  6. painter.drawRect(rect()); //paint the background with linear gradient
  7. painter.end();
  8.  
  9. makeCurrent();
  10.  
  11. glMatrixMode(GL_MODELVIEW);
  12. glPushMatrix();
  13. glShadeModel(GL_SMOOTH);
  14. glEnable(GL_DEPTH_TEST);
  15. glEnable(GL_CULL_FACE);
  16. glEnable(GL_LIGHTING);
  17. glEnable(GL_LIGHT0);
  18. glEnable(GL_MULTISAMPLE);
  19. static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 };
  20. glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
  21.  
  22. setupViewport(width(), height());
  23.  
  24. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  25. glLoadIdentity();
  26. glTranslatef(0.0, 0.0, -10.0);
  27. glRotatef(xRot / 16.0, 1.0, 0.0, 0.0);
  28. glRotatef(yRot / 16.0, 0.0, 1.0, 0.0);
  29. glRotatef(zRot / 16.0, 0.0, 0.0, 1.0);
  30.  
  31. glPushAttrib(GL_ALL_ATTRIB_BITS);
  32. qglColor(QColor(255, 239, 191));
  33. glBegin(GL_TRIANGLES); //draw 2 random triangles to test
  34. glVertex3f(+0.3, +0.3, -0.3);
  35. glVertex3f(-0.3, +0.3, -0.3);
  36. glVertex3f(+0.3, -0.3, -0.3);
  37. glVertex3f(-0.3, -0.3, -0.3);
  38. glVertex3f(+0.3, -0.3, +0.3);
  39. glVertex3f(-0.3, -0.3, +0.3);
  40. glEnd();
  41. glPopAttrib();
  42.  
  43. glShadeModel(GL_FLAT);
  44. glDisable(GL_CULL_FACE);
  45. glDisable(GL_DEPTH_TEST);
  46. glDisable(GL_LIGHTING);
  47.  
  48. glMatrixMode(GL_MODELVIEW);
  49. glPopMatrix();
  50. }
To copy to clipboard, switch view to plain text mode