Results 1 to 4 of 4

Thread: QGLWidget and QThread, how to render in separate thread?

  1. #1
    Join Date
    Sep 2008
    Posts
    25
    Thanks
    3
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QGLWidget and QThread, how to render in separate thread?

    I've found a nice tutorial on multithreaded OpenGL rendering on QGLWidget:
    http://doc.trolltech.com/qq/qq06-gli...glapplications
    But I have some questions about it:
    1.
    Qt Code:
    1. class GLThread : public QThread
    To copy to clipboard, switch view to plain text mode 
    Is it safe to modify GLThread's members:
    Qt Code:
    1. bool doRendering;
    2. bool doResize;
    3. int w;
    4. ...
    To copy to clipboard, switch view to plain text mode 
    when GLThread thread is running and reads each variable every iteration?
    I'm talking about

    Qt Code:
    1. void GLWidget::resizeEvent(QResizeEvent *evt)
    2. { /*glt is GLThread's object*/
    3. glt.resizeViewport(evt->size());
    4. }
    To copy to clipboard, switch view to plain text mode 


    2. Is it safe to modify more complex GLThread's members when GLThread thread is running? E.g.
    Qt Code:
    1. class MyComplexGLThreadMemberClass {
    2. int a;
    3. double b;
    4. vector<int> c;
    5. };
    To copy to clipboard, switch view to plain text mode 

    3.
    Qt Code:
    1. class GLWidget : public QGLWidget
    To copy to clipboard, switch view to plain text mode 

    Is it safe to call GLWidget's methods from a running GLThread thread?
    Qt Code:
    1. void GLThread::run()
    2. {
    3. glw->makeCurrent();
    4. while (doRendering) {
    5. // Rendering code goes here
    6. glw->swapBuffers();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    Here is my source code for Qt4.7 and exe file built with Qt4.7, you'll need Qt dlls to run it.
    TestGl.zip
    TestGl_exe.zip

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGLWidget and QThread, how to render in separate thread?

    Long story short - if you want to be safe, don't use multiple threads with any widget at all. If you want to be relatively safe, substitute those variables with QAtomicInts and use mutexes to protect complex thread-unsafe members. Never touch any of QWidget's methods from a worker thread.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2008
    Posts
    25
    Thanks
    3
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGLWidget and QThread, how to render in separate thread?

    So you do guarantee that this tutorial in QQ is totally incorrect? That I can't call glw->makeCurrent(); glw->swapBuffers(); in separate thread?
    How do I make my slow rendering not to influence UI then?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGLWidget and QThread, how to render in separate thread?

    Quote Originally Posted by DIMEDROLL View Post
    So you do guarantee that this tutorial in QQ is totally incorrect?
    I didn't say anything like that. I said you shouldn't be using multiple threads with widgets.

    That I can't call glw->makeCurrent(); glw->swapBuffers(); in separate thread?
    That should be possible because you are not interacting with the widget in any way - the gl context and the widget are two separate entities.

    How do I make my slow rendering not to influence UI then?
    I would start by determining why the rendering is slow in the first place. Adding a thread will not make your app magically fast.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 8
    Last Post: 9th November 2010, 21:11
  2. Render QWidget within QGLWidget
    By crazymonkey in forum Newbie
    Replies: 29
    Last Post: 26th September 2010, 13:54
  3. Replies: 1
    Last Post: 7th May 2010, 17:20
  4. QGLWidget Render Text in the foreground
    By arpspatel in forum Qt Programming
    Replies: 1
    Last Post: 11th April 2010, 11:35
  5. run a QMessageBox from a separate QThread
    By franco.amato in forum Qt Programming
    Replies: 2
    Last Post: 6th April 2010, 04:33

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.