Results 1 to 2 of 2

Thread: OpenGL and QPainter

  1. #1
    Join Date
    Mar 2010
    Posts
    10
    Thanks
    2
    Qt products
    Qt4

    Unhappy OpenGL and QPainter

    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 

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: OpenGL and QPainter

    Maybe the wolfenqt example can help you there.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

Similar Threads

  1. QPainter(&QPrinter) & QPainter(&QImage) communication
    By gufeatza in forum Qt Programming
    Replies: 2
    Last Post: 2nd February 2010, 07:25
  2. Replies: 0
    Last Post: 6th December 2009, 00:41
  3. Replies: 5
    Last Post: 7th September 2009, 20:57
  4. Problem when mixing openGL and QPainter
    By sanjayshelke in forum Qt Programming
    Replies: 9
    Last Post: 21st March 2008, 12:49
  5. Replies: 3
    Last Post: 30th April 2006, 19:22

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.