Results 1 to 2 of 2

Thread: OpenGL Render issue when using QGLWidget with QGraphicsView

  1. #1
    Join Date
    May 2010
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default OpenGL Render issue when using QGLWidget with QGraphicsView

    I created a simple QGraphicsView-inherited class that uses a QGLWidget-inherited object as a viewport.

    I want the contents of the QGLWidget to be rendered as the background of the QGraphicsView.

    Sounds simple, right?

    It works fine as long as I don't add any QGraphicItems to the scene -- the graphic that I have defined in my QGLWidget gets drawn as the background of the QGraphicsView.
    However, as soon as I add an item to the scene, the background does not render properly.

    Everything works smoothing if my QGLWidget renders in direct mode (e.g. glBegin(), glEnd()). However, if I use vertex arrays or VBOs, I get weird anomalies in my render.

    My theory is that QGraphicsView is modifying the OpenGL context somewhere and not resetting its state, such that when I go to render in my QGLWidget, I am in an unexpected state.

    Can someone please try the code I have posted here?
    It uses the "GLWidget" class from <qt-dir>/examples/opengl/hellogl/qlwiget.* , and that in turns relies on <qt-dir>/examples/opengl/share/qtlogo.*

    If you comment out the 'addEllipse' line, you should see the Qt graphic as the background, but if you keep it uncommented, you should see something wonky.

    I'm using qt4.6.2 and Fedora11 64-bit on a higher-end NVIDIA card.

    Qt Code:
    1. #include <QtGui>
    2. #include "glwidget.h" // from <qt-dir>/examples/opengl/hellogl/glwidget.*
    3.  
    4. // QGraphicsView that uses a GLWidget as its viewport
    5. class GLGraphicsView : public QGraphicsView
    6. {
    7. public:
    8. GLGraphicsView(QGraphicsScene * scene, QWidget * parent = 0 ) : QGraphicsView(scene, parent)
    9. {
    10. mpGLWidg = new GLWidget(NULL);
    11. setViewport(mpGLWidg);
    12. };
    13.  
    14. protected:
    15.  
    16. void drawBackground(QPainter *p, const QRectF &r)
    17. {
    18. mpGLWidg->updateGL();
    19. QGraphicsView::drawBackground(p, r);
    20. };
    21. private:
    22. GLWidget* mpGLWidg; // use GLWidget class from <qt-dir>/examples/opengl/hellogl/glwidget.*
    23. // it depends on <qt-dir>/examples/opengl/shared/qtlogo.*
    24. };
    25.  
    26.  
    27. int main(int argc, char *argv[])
    28. {
    29. QApplication app(argc, argv);
    30.  
    31. GLGraphicsView gView(&scene);
    32.  
    33. // comment this line out, and the logo will appear:
    34. scene.addEllipse(QRectF(10, 20, 30, 40), QPen(Qt::green), QBrush(Qt::SolidPattern));
    35.  
    36. gView.resize(800,600);
    37. gView.show();
    38.  
    39. int ret = app.exec();
    40. return ret;
    41. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2010
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: OpenGL Render issue when using QGLWidget with QGraphicsView

    For anyone who runs into this problem, I suggest taking a look at the 'boxes' demo: http://doc.trolltech.com/4.6//demos-boxes.html

    The design I have above is slightly skewed, and the demo shows a much better way to go about putting an OpenGL background behind a bunch of Qt widgets.
    (basically, you inherit from QGraphicsScene instead of QGLWidget to add custom OpenGL code)

Similar Threads

  1. QGLWidget Render Text in the foreground
    By arpspatel in forum Qt Programming
    Replies: 1
    Last Post: 11th April 2010, 12:35
  2. Opengl issue with Qt4.4.3
    By j0rt4g4 in forum Newbie
    Replies: 13
    Last Post: 2nd December 2008, 13:36
  3. QGraphicsView + OpenGL -> Pixmap rendering issue
    By JoergRe in forum Qt Programming
    Replies: 2
    Last Post: 20th May 2008, 19:41
  4. QGraphicsView Render() Issue
    By paul567 in forum Qt Programming
    Replies: 12
    Last Post: 16th November 2007, 13:26
  5. OpenGl Issue
    By vermarajeev in forum Qt Programming
    Replies: 6
    Last Post: 7th April 2007, 06:28

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.