Now we render the background of the scene with our own OpenGL calls..

Qt Code:
  1. #ifndef MAIN_H
  2. #define MAIN_H
  3.  
  4. #include <QtCore>
  5. #include <QtGui>
  6. #include <QtOpenGL>
  7.  
  8. using namespace std;
  9.  
  10. class OpenGLScene : public QGraphicsScene
  11. {
  12. public:
  13. OpenGLScene() {
  14.  
  15. }
  16. protected:
  17. void drawBackground(QPainter *painter, const QRectF &);
  18. };
  19.  
  20. void OpenGLScene::drawBackground(QPainter *painter, const QRectF &)
  21. {
  22. /*if (painter->paintEngine()->type() != QPaintEngine::OpenGL) {
  23.   qWarning("OpenGLScene: drawBackground needs a "
  24.   "QGLWidget to be set as viewport on the "
  25.   "graphics view");
  26.   return;
  27.   }*/
  28. //
  29. QColor m_backgroundColor = QColor("yellow");
  30. glClearColor(m_backgroundColor.redF(),
  31. m_backgroundColor.greenF(),
  32. m_backgroundColor.blueF(), 1.0f);
  33. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  34. }
  35.  
  36.  
  37. class MainWindow : public QMainWindow
  38. {
  39. public:
  40. MainWindow(QWidget *parent = 0);
  41. private:
  42. OpenGLScene *scene;
  43. QGraphicsView *graphicsView;
  44. };
  45.  
  46.  
  47.  
  48. MainWindow::MainWindow(QWidget *parent) :
  49. QMainWindow(parent)
  50. {
  51. scene = new OpenGLScene();
  52.  
  53. QPushButton* btn = new QPushButton("Test");
  54. scene->addWidget(btn);
  55.  
  56. graphicsView = new QGraphicsView(this);
  57. graphicsView->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
  58. graphicsView->setScene(scene);
  59. graphicsView->setGeometry(QRect(50, 50, 500, 300));
  60. graphicsView->show();
  61.  
  62. this->setCentralWidget(graphicsView);
  63.  
  64. }
  65.  
  66. #endif // MAIN_H
To copy to clipboard, switch view to plain text mode 
Aah! Something is wrong with this check! Did produce the same warning here. But the OpenGL stuff obviously works!

Joh