Hi all,
i'm trying to capture an image from a QGLWidget's back buffer (so, no redraw must be seen from user) into a QImage having a transparent background, but the alpha value always return 255. Here's what i did (till now):

I have reimplemented QGLWidget to myGLWidget:

Qt Code:
  1. myGLWidget::myGLWidget
  2. {
  3. setAutoBufferSwap(false);
  4. takingSnapshot = false; // member variable. When taking snapshot turns true
  5. }
  6.  
  7. void myGLWidget::initializeGL()
  8. {
  9. glClearColor(1., 1., 1., 0.);
  10. }
  11.  
  12. void myGLWidget::resizeGL(int ww, int hh)
  13. {
  14. glViewport(0, 0, (GLint)ww, (GLint)hh);
  15. }
  16.  
  17. void myGLWidget::paintGL()
  18. {
  19. qDebug() << "paintGL";
  20.  
  21. glPushAttrib(GL_TRANSFORM_BIT);
  22.  
  23. glMatrixMode(GL_PROJECTION);
  24. glPushMatrix();
  25. glLoadIdentity();
  26. glMatrixMode(GL_MODELVIEW);
  27. glPushMatrix();
  28. glLoadIdentity();
  29. glOrtho(-1., 1., -1., 1., -1., 1.);
  30.  
  31.  
  32. /* Clear screen */
  33. if (takingSnapshot) {
  34. glEnable(GL_BLEND);
  35. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  36. glClearColor(0., 0., 1., 0.0f);
  37. glClear(GL_COLOR_BUFFER_BIT);
  38. glDisable(GL_BLEND);
  39. } else {
  40. glClearColor(1., 1., 0., 1.);
  41. glClear(GL_COLOR_BUFFER_BIT);
  42. }
  43.  
  44.  
  45. /* Begin Draw */
  46. glColor4f(1., 0., 0., 1.);
  47. glLineWidth(10);
  48.  
  49. glBegin(GL_LINES);
  50. glVertex2f(-0.3, 0.0);
  51. glVertex2f(0.3, 0.0);
  52. glEnd();
  53.  
  54. glEnable(GL_BLEND);
  55. glColor4f(0., 1., 0., 0.4);
  56. glRotatef(90., 0., 0., 1.);
  57. glBegin(GL_LINES);
  58. glVertex2f(-0.3, 0.0);
  59. glVertex2f(0.3, 0.0);
  60. glEnd();
  61.  
  62. glPopMatrix();
  63. glMatrixMode(GL_PROJECTION);
  64. glPopMatrix();
  65.  
  66. glPopAttrib();
  67. glDisable(GL_BLEND);
  68.  
  69. if (!takingSnapshot) {
  70. swapBuffers();
  71. } else {
  72. qDebug() << "Finished drawing after snapshot\n";
  73. }
  74. }
To copy to clipboard, switch view to plain text mode 


And here is my mainWindow ( MainWindow : public QMainWindow):

Qt Code:
  1. MainWindow::MainWindow(QWidget *parent)
  2. : QMainWindow(parent)
  3. {
  4. mainFrame = new QFrame(this);
  5. setCentralWidget(mainFrame);
  6.  
  7. mainLay = new QVBoxLayout(mainFrame);
  8. mainFrame->setLayout(mainLay);
  9.  
  10. glw = new myGLWidget(mainFrame);
  11. snap = new QPushButton("Snap", mainFrame);
  12. label = new QLabel(mainFrame);
  13.  
  14. mainLay->addWidget(glw);
  15. mainLay->addWidget(snap);
  16. mainLay->addWidget(label);
  17.  
  18. mainFrame->show();
  19. glw->show();
  20. snap->show();
  21. label->show();
  22.  
  23. QObject::connect(snap,SIGNAL(clicked()),this,SLOT(takeImage()));
  24. }
  25.  
  26. static QImage createQImage(const unsigned char *image2, int ww, int hh)
  27. {
  28. /* Do some stuff here with "image2" */
  29. /* and return the final image */
  30. }
  31.  
  32. void MainWindow::takeImage()
  33. {
  34. int ww = glw->width();
  35. int hh = glw->height();
  36. QImage finalImage;
  37. GLubyte *image = (GLubyte *)malloc(ww * hh * sizeof(GLubyte) * 4);
  38.  
  39. glw->takingSnapshot = true;
  40. glPushAttrib(GL_PIXEL_MODE_BIT | GL_COLOR_BUFFER_BIT); // readBuffer + drawBuffer
  41. glReadBuffer(GL_BACK);
  42. glDrawBuffer(GL_BACK);
  43. qDebug() << "takeImage, will call updateGL";
  44.  
  45. glw->updateGL();
  46. glReadPixels(0,0,ww,hh,GL_RGBA,GL_UNSIGNED_BYTE,image);
  47.  
  48. finalImage = createQImage(image, ww, hh);
  49.  
  50. if (!finalImage.isNull()) {
  51. QPixmap pix;
  52. pix.convertFromImage(finalImage);
  53. label->setPixmap(pix);
  54. }
  55. glPopAttrib();
  56. glw->takingSnapshot = false;
  57. if (image) free(image);
  58. }
To copy to clipboard, switch view to plain text mode 

In function "createQImage", when i loop the pixels of "image2" and test the alpha value, as i said before, it is always 255! In order to check if i do something wrong with the created QImage in "createQImage", i forced a block of pixels to have 0 alpha and my final QImage had that block with 100% transparency.

Any ideas?