Hi,
I saw https://doc.qt.io/archives/qq/qq26-openglcanvas.html and tried to alter the code of the example "Cube OpenGL ES 2.0" so it uses a QGraphicsView. I only get black screen. I'm on Ubuntu 14.04 / Qt5.3.2.

main.cpp:
Qt Code:
  1. #include <QApplication>
  2. #include <QGLWidget>
  3.  
  4. #include "scene.h"
  5. #include "view.h"
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9. QApplication a(argc, argv);
  10. QGLWidget *widget = new QGLWidget(QGLFormat(QGL::SampleBuffers));
  11. widget->setAutoFillBackground(false);
  12. widget->makeCurrent();
  13. Scene *scene= new Scene();
  14. View *mainView = new View();
  15. mainView->setViewport(widget);
  16. mainView->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
  17. mainView->setScene(scene);
  18. mainView->show();
  19. mainView->resize(800,800);
  20. return a.exec();
  21. }
To copy to clipboard, switch view to plain text mode 

scene.cpp:
Qt Code:
  1. #include "scene.h"
  2.  
  3. #include <QGraphicsScene>
  4. #include <QGraphicsSceneMouseEvent>
  5. #include <QTimer>
  6.  
  7. Scene::Scene() : angularSpeed(0)
  8. {
  9. initializeGLFunctions();
  10. glClearColor(0.0f,0.0f,0.0f,1.0f);
  11. initShaders();
  12. initTextures();
  13. glEnable(GL_DEPTH_TEST);
  14. glEnable(GL_CULL_FACE);
  15. geometries.init();
  16. timer.start(12, this);
  17. }
  18. void Scene::drawBackground(QPainter *painter, const QRectF &rect)
  19. {
  20. painter->beginNativePainting();
  21. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  22. QMatrix4x4 matrix;
  23. matrix.translate(0.0, 0.0, -5.0);
  24. matrix.rotate(rotation);
  25. program.setUniformValue("mvp_matrix", projection * matrix);
  26. program.setUniformValue("texture", 0);
  27. geometries.drawCubeGeometry(&program);
  28. }
  29. void Scene::initShaders()
  30. {
  31. program.addShaderFromSourceFile(QGLShader::Vertex, ":/vshader.glsl");
  32. program.addShaderFromSourceFile(QGLShader::Fragment, ":/fshader.glsl");
  33. program.link();
  34. program.bind();
  35. }
  36. void Scene::initTextures()
  37. {
  38. glEnable(GL_TEXTURE_2D);
  39. textureData = new QOpenGLTexture(QImage(":/cube.png"));
  40. textureData->bind();
  41. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  42. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  43. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  44. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  45. }
  46. void Scene::timerEvent(QTimerEvent *)
  47. {
  48. angularSpeed *= 0.99;
  49. if (angularSpeed < 0.01) {
  50. angularSpeed = 0.0;
  51. } else {
  52. rotation = QQuaternion::fromAxisAndAngle(rotationAxis, angularSpeed) * rotation;
  53. update();
  54. }
  55. }
  56. void Scene::mousePressEvent(QGraphicsSceneMouseEvent *event)
  57. {
  58. mousePressPosition = QVector2D(event->scenePos());
  59. }
  60. void Scene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
  61. {
  62. QVector2D diff = QVector2D(e->scenePos()) - mousePressPosition;
  63. QVector3D n = QVector3D(diff.y(), diff.x(), 0.0).normalized();
  64. qreal acc = diff.length() / 100.0;
  65. rotationAxis = (rotationAxis * angularSpeed + n * acc).normalized();
  66. angularSpeed += acc;
  67. }
To copy to clipboard, switch view to plain text mode 

view.h
Qt Code:
  1. #include <QGraphicsView>
  2. #include <QResizeEvent>
  3. #include "geometryengine.h"
  4.  
  5.  
  6. class View : public QGraphicsView
  7. {
  8. Q_OBJECT
  9. protected:
  10. void resizeEvent(QResizeEvent *event){
  11. if (scene())
  12. scene()->setSceneRect(
  13. QRect(QPoint(0, 0), event->size()));
  14. QGraphicsView::resizeEvent(event);
  15. }
  16. public:
  17. View(){}
  18. };
  19.  
  20. #endif // VIEW_H
To copy to clipboard, switch view to plain text mode 
Any ideas where it went wrong?