Important: Please read the Qt Code of Conduct -

QThread real control

  • Hello guys :),

    I would like to parallelize my simulation... but I really have gone crazy because every method I try for using QThread fails. Either by not having access to the object inherited from QThread because it doesn't support Widgets, or because of a segmentation fault that I don't understand and doesn't make sense at all!

    Structure of my simulation:

    The structure of a simulation program I'm creating is as follows:

    MainWindow contains an OpenGLWidget that contains an object that has the simulation tools, parameters, equations and functions. MainWindow contains also options Dialogs that control the parameters in OpenGLWidget and the simulation object contained in it.

    The simulation starts with initial values. OpenGLWidget draws the initial values. The simulation object which is inside the OpenGLWidget has a function called advanceTimeStep(). This function advances the simulation to the next step by solving some equations. After drawing the initial values in OpenGLWidget, OpenGLWidget calles the function advanceTimeStep() which advances the simulation and gets new values to be drawn in OpenGLWidget.

    To ensure that the speed of the rendering is independent of the computer speed, the time is measured between every two contiguous renderings of OpenGL. If more than 1 ms has passed, then more advanceTimeStep()s are called to substitute the time lost in rendering and retain a constant speed for the simulation.


    I would like to let openGL process in a thread, and the simulation in another thread. So I'm not sure to do this. The first thing I did is putting the simulation object inside a thread. Which is a class inherited from QThread. So in the class body, I defined the simulation object as a pointer. And in the function run(), I defined the memory allocation operator "new" for my simulation object. This didn't work because I didn't know how to use advancedTimeStep() in it!!!

    The call was done by calling thead::start() function.

    I've tried also many other ways like putting advanceTimeStep() in run(). And failed also in using the thread as a widget, which I need actually where I could have a thread for openGL.


    -What's the best way to parallelise such a system?
    -Is there a way to have OpenGL in a thread and the simulation object in another thread and have them both effectively linked to exchange data?
    -Does calling the function thread::start() keep looping run()??? how would I use thread::start() in my case??

    Thanks you :)

  • Come on guys... anything that helps :(

  • Hi TheDestroyer,
    I have only limited experience with threading, but it wasn't very complicated.
    "Have you started reading with that part? ":

  • I was checking some of my code. You better derive a class from QThread. The derived class needs to have a run method for actually doing whatever it has to do.
    My run looks like:
    void ThreadSend :: run ()

    The forever statement is not a joke. It is part of Qt.

    Maybe you should have a look to this "example":

Log in to reply