Results 1 to 3 of 3

Thread: Rendering to a QGLWidget from a QThread - makeCurrent fails [solved]

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Rendering to a QGLWidget from a QThread - makeCurrent fails [solved]

    Hi there,
    I'm currently writing a Qt4 GUI for an emulator and need to offload the rendering of a QGLWidget to a separate QThread. The reason for this is that since our GPU emulation is using OpenGL for rendering, we'd have to run emulation on the GUI thread, which is kinda awkward. I don't want to restructure the underlying emulator code (especially since it's supposed to be toolkit-independent) to avoid this (if that's even possible), so I figured the best way would be to leave the GUI thread as it is and let the emulation run on a separate QThread.
    This article suggests that this idea should be perfectly feasible. However, when I'm trying to implement it like described in the article, I get an "QGLContext::makeCurrent(): Failed." error whenever I'm calling glwidget->makeCurrent() from the QThread (glwidget->context()->isValid() returns true though). I've tried coding a minimal application that will test the implementation, and it also outputs makeCurrent errors:

    Qt Code:
    1. #include <QtGui>
    2. #include <QtOpenGL>
    3. #include <X11/Xlib.h>
    4.  
    5. class MyGLWidget;
    6. class MyThread : public QThread
    7. {
    8. public:
    9. MyThread(MyGLWidget* _glw);
    10. void run();
    11.  
    12. private:
    13. MyGLWidget* glw;
    14. };
    15.  
    16. class MyGLWidget : public QGLWidget
    17. {
    18. public:
    19. MyGLWidget(QWidget* parent = NULL);
    20.  
    21. void start_rendering() { thread.start(); }
    22. private:
    23. MyThread thread;
    24. };
    25.  
    26. MyThread::MyThread(MyGLWidget* _glw) : glw(_glw) {}
    27. MyGLWidget::MyGLWidget(QWidget* parent) : QGLWidget(parent), thread(this) {}
    28.  
    29.  
    30. void MyThread::run()
    31. {
    32. while(1) {
    33. glw->makeCurrent(); // error
    34. glClear(GL_COLOR_BUFFER_BIT);
    35. glw->swapBuffers();
    36. glw->doneCurrent();
    37. }
    38. }
    39.  
    40. int main(int argc, char* argv[])
    41. {
    42. QCoreApplication::setAttribute(Qt::AA_X11InitThreads); // or just XInitThreads();
    43. QApplication app(argc, argv);
    44.  
    45. MyGLWidget* glw = new MyGLWidget(&win);
    46. win.setCentralWidget(glw);
    47. win.show();
    48. glw->start_rendering();
    49.  
    50. return app.exec();
    51. }
    To copy to clipboard, switch view to plain text mode 
    Note that I'm running this on an openSUSE Linux system right now. I haven't had a chance to test it on a Windows machine, yet, but will do so once I get a chance.

    Anyway, I would be REALLY thankful if anyone could help me out with this one, I've spent so many hours on this already without finding a solution. Thanks in advance!
    Last edited by neobrain; 15th March 2012 at 02:19.

  2. #2
    Join Date
    Feb 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Rendering to a QGLWidget from a QThread - makeCurrent fails

    I just solved the problem.

    For reference, I thought it wasn't necessary to overload the paintEvent and resizeEvent methods in QGLWidget. However, you need to do that because otherwise QGLWidget will internally keep calling makeCurrent, thus causing the makeCurrent calls in the QThread to fail.

  3. #3
    Join Date
    Nov 2014
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Rendering to a QGLWidget from a QThread - makeCurrent fails

    I just registered to say Thank you, No info anywhere that it NEEDS to be overloaded, even in documentation, so frustrating to find it only after so many hours of search.

Similar Threads

  1. Replies: 1
    Last Post: 27th June 2011, 22:37
  2. Video Rendering fails inside QGraphicsScene
    By sschilz in forum Qt Programming
    Replies: 0
    Last Post: 30th August 2010, 22:29
  3. QGLWidget makeCurrent & makeOverlayCurrent
    By chameleon in forum Qt Programming
    Replies: 0
    Last Post: 9th May 2010, 15:05
  4. 3D rendering on Ubuntu using QGLWidget
    By sanjayshelke in forum Qt Programming
    Replies: 2
    Last Post: 14th January 2009, 13:05
  5. Tile rendering with QGLWidget
    By h123 in forum Qt Programming
    Replies: 2
    Last Post: 10th November 2008, 23:27

Tags for this Thread

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.