Results 1 to 4 of 4

Thread: QGLWidget & QGLContext & QThread

  1. #1
    Join Date
    Apr 2011
    Posts
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QGLWidget & QGLContext & QThread

    Hi,

    I have written a class MyGLWidget which is inherited from QGLWidget. It has all the mouse handling functions for rotating/zooming/panning.

    I want to paint on this MyGLWidget from two different places in the program (by not using function pointers for paintGL function). I do not have idea that how can I use QGLContext and makeCurrent stuff?


    Also, how can I create QGLContext with QThread if I want to use QThread for drawing from two different places? How can I share a QGLContext?

    I studied the QThread and QGLWidget example on article http://doc.trolltech.com/qq/qq06-glimpsing.html and seems fine to me. But how can I share QGLContext from two different threads?

    Can anybody please throw some light on this?
    Thank you.

    --
    regards,
    eye51

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGLWidget & QGLContext & QThread

    Hi!

    I would recommend against sharing a glcontext between different threads! You can't really do anything in parallel. The context has to be blocked and released by each thread.

    See: http://www.equalizergraphics.com/doc...OpenGLFAQ.html

    Try to change your design in such a way, that your background-threads pre-calculate everything possible (e.g. textures etc) and make the actual calls to the glContext from just one thread.

    You can use signals and slots to communicate between the worker objects of your threads.

    Johannes

  3. #3
    Join Date
    Apr 2011
    Posts
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGLWidget & QGLContext & QThread

    Hi Johannes,

    Thank you very much for your ideas on my question.

    I agree with you. As you said, one thread should pre-compile all the required drawings and another thread should draw those heavy GL drawings. The reason I was asking to share a glcontext was that my application may require huge visualization data to draw which may not be feasible on a single core master node.

    Also, can you please elaborate on how can I create a glcontext for each thread and use them? In my application, actually I wanted to draw different things (grid or contours) on a single glwidget. But now if I use two threads and create two glcontext for each, I need to create two glwidget windows (?)

    Also, can you provide a small example (or suggest a link) which uses qglwidget and qthread using signal and slot mechanism?

    Thanks for taking out time for me.

    --
    regards,
    eye51

  4. #4
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGLWidget & QGLContext & QThread

    Hi!

    How to use multiple glWidgets and contexts and update each from their own thread is described perfectly well in the link you first posted!

    As to the signal slot mechanism, it doesn't matter if its a qglwidget or something else... The general idea is the following:

    Qt Code:
    1. // Just wrote this down out of my head ... typos included.
    2. class Worker : public QObject
    3. { Q_OBJECT
    4. signals:
    5. void resultReady(Data* d)
    6. public:
    7. Worker() ..
    8. public slots:
    9. void calculate()
    10. {
    11. emit resultReady(d)
    12. }
    13. };
    14.  
    15. Worker* w = new Worker();
    16. QThread* thread = new QThread();
    17. w.moveToThread(thread);
    18. connect(w,SIGNAL(resultReady(Data*)),this,SLOT(showResult(Data*)));
    19. thread->start();
    20. // To start the worker you need to invoke the method through the event
    21. // loop of the threads its living in
    22. QMetaObject::invokeMethod(w,"calculate");
    To copy to clipboard, switch view to plain text mode 
    The this object in the above code, could be your subclassed QGLWidget. I don't know enough about your situation. How do you toggle a redraw? You would need to invoke the calculate method from there... And make sure that the Data you pass along is not overwritten by the Worker inadvertently. Either create new data variable every time or if that is to expensive make it doublebuffered and protect each halfbuffer with a mutex, which should virtually never block because the drawing should be much faster then the calculation. What you gain by this approach is that you can have a multitude of worker threads preparing stuff for the glwidget to draw.

    Johannes
    Last edited by JohannesMunk; 3rd April 2011 at 17:54. Reason: updated contents

Similar Threads

  1. QGLWidget and QThread, how to render in separate thread?
    By DIMEDROLL in forum Qt Programming
    Replies: 3
    Last Post: 2nd December 2010, 20:56
  2. QGLContext problems
    By bhuang in forum Qt Programming
    Replies: 1
    Last Post: 25th August 2010, 09:18
  3. QGLContext Issue
    By parmar in forum Qt Programming
    Replies: 5
    Last Post: 18th June 2010, 11:25
  4. Replies: 4
    Last Post: 17th October 2009, 23:31
  5. overlay QGLContext
    By showhand in forum Qt Programming
    Replies: 3
    Last Post: 10th April 2007, 17:51

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.