I just tried setting up a subclass of QGLWidget in my application.
When i use the QPainter (in paintEvent), i can draw a line ok but drawing a pixmap just results in a white rectangle.
But then i copied over the OpenGL code from my old application, which uses SOIL to load a texture then render it on a quad, that works.

So, what is the difference? I'm guessing it has something to do with the parameters i am setting on initialization, or the way i am using the images as textures (how does QPainter::drawPixmap render the image in OpenGL? I'll have a look at the source code).
I would like to use QPainter since it's easier, but since i have already written most of the graphics code using plain OpenGL functions, i do have a way of solving my problem.


The code...
Qt Code:
  1. GLWidget::GLWidget(QWidget* parent) :
  2. QGLWidget(QGLFormat(QGL::SampleBuffers), parent),
  3. testImage("C:\\blah\\Test.png"),
  4. testTexId(0) {
  5. setAutoFillBackground(false);
  6. }
  7.  
  8. //--- This does NOT work. ------------------------
  9. void GLWidget::paintEvent(QPaintEvent*) {
  10. QPainter painter;
  11. painter.begin(this);
  12. painter.setRenderHint(QPainter::Antialiasing);
  13. painter.setBackground(Qt::black);
  14. painter.setPen(QPen(Qt::red, 2));
  15. painter.drawLine(0, 0, 300, 300);
  16. painter.drawPixmap(50, 50, testImage);
  17. painter.end();
  18. }
  19. //--------------------------------------------------
  20.  
  21.  
  22. //--- But this DOES. -----------------------------
  23. void GLWidget::initializeGL() {
  24. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  25. glClearDepth(1.0f);
  26. glShadeModel(GL_FLAT);
  27. glEnable(GL_POINT_SMOOTH);
  28. glEnable(GL_TEXTURE_2D);
  29. glDisable(GL_DEPTH_TEST);
  30. glEnable(GL_BLEND);
  31. glEnable(GL_COLOR_MATERIAL);
  32. glEnable(GL_COLOR_LOGIC_OP);
  33. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  34. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
  35. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  36. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  37.  
  38. // Set up orthographic view
  39. glMatrixMode(GL_PROJECTION);
  40. glPushMatrix();
  41. glLoadIdentity();
  42. glOrtho(0, 800, 600, 0, -1, 1);
  43. glMatrixMode(GL_MODELVIEW);
  44. glPushMatrix();
  45. glLoadIdentity();
  46.  
  47. // The SOIL image loading code
  48. unsigned char* img = NULL;
  49. int ch, width, height;
  50.  
  51. img = SOIL_load_image("C:\\blah\\Test.png", &width, &height, &ch, SOIL_LOAD_AUTO);
  52. testTexId = SOIL_create_OGL_texture(img, width, height, ch, SOIL_CREATE_NEW_ID, SOIL_FLAG_TEXTURE_REPEATS);
  53. SOIL_free_image_data(img);
  54. }
  55.  
  56. void GLWidget::paintGL() {
  57. glDisable(GL_TEXTURE_2D);
  58. glBegin(GL_LINES);
  59. glVertex2i(0, 0); glVertex2i(300, 300);
  60. glEnd();
  61. glEnable(GL_TEXTURE_2D);
  62.  
  63. glPushMatrix();
  64. glTranslated(10, 10, 0);
  65. glRotated(12.0f, 0.0f, 0.0f, 1.0f);
  66.  
  67. // Draw quad with texture
  68. glBindTexture(GL_TEXTURE_2D, testTexId);
  69. glBegin(GL_QUADS);
  70. glTexCoord2f( 0, 0); glVertex2i(0, 0);
  71. glTexCoord2f( 0, 1); glVertex2i(0, 200);
  72. glTexCoord2f(1, 1); glVertex2i(200, 200);
  73. glTexCoord2f(1, 0); glVertex2i(200, 0);
  74. glEnd();
  75.  
  76. glPopMatrix();
  77. }
  78. //--------------------------------------------------
To copy to clipboard, switch view to plain text mode