Important: Please read the Qt Code of Conduct -

How to let qtimer execute if under while loop

  • How to let qtimer execute if under while loop from same thread?

    void set_timer()
    QTimer::singleShot(500, Qt::PreciseTimer, SLOT( execute_timer() ));

    void execute_timer()

    Can event loop help?

  • Lifetime Qt Champion


    What are you exactly trying to achieve ? What you wrote here could be replace by a non single shot QTimer

  • Is it possible for Timer to pause operation in while loop and update gui/process events then continue from same point while loop?

  • Moderators

    @Q139 said:

    Can event loop help?

    Your while loop is an infinite loop. It blocks Qt's event loop.

    Is it possible for Timer to pause operation in while loop and update gui/process events then continue from same point while loop?


    I recommend you redesign your program.

    If you describe what you want to do, we could help you with your design.

  • Program has gui , calculating part and opengl rendering working in same thread.
    Sometimes it does long calculations that are started by while loop , gui update and rendering works if using processevents comand under while loop.
    Easyest solution i can think is use some timer in while loop that detects if time passed and forces refresh but it may not be proper.
    Is it possible to put qtimer in other thread and update gui from there or it wont be executed because program is operating while loop?
    Maybe to make opengl seperate thread gui seperate thread and worker thread.
    It probably complex to make 3 threads work as many variables are shared by gui , opengl rendering and calculation part.
    What would be best solution to preserve computing power and also have responsive gui and opengl?

    Best if opengl low fps wont slow gui and while loop without forced event process wont stop both from beign updated.
    also calculating part could be run multiple thread with different settings on each thread but at this point my knowledge not too good on how to avoid threading problems or how to initialize opengl and gui to seperate threads correctly.

    This is int main

    int main(int argc, char *argv[])
    QApplication a(argc, argv);
    MainWin w;

    QSurfaceFormat format;
    //antializing samples
    TriangleWindow window;
    window.resize(800, 600);
    window.setAnimating(true);;  //gl;       //ui
    return a.exec();


  • Lifetime Qt Champion

    If you have such heavy calculation then you should put that part in a secondary thread. The rest can stay in the same thread.

  • Also would be good if opengl seperate thread , sometimes when lot of data displayed fps drop very low and slow gui but it only happenened so far on atom powered laptop or older hardware.

    Can please guide to some materials how to initialize opengl to seperate thread and how to initialize worker threads?

  • I think @SGaist meant the 'long calculation' part of your program in a separate thread.

    The GUI should not be split into different threads (including the openGl parts). I am not ever sure if you can actually have Qt GUI elements run in different threads (like QWidget). If this is possible you should avoid it. Threads should be used for long calculations or other non-GUI functions.

    If the openGl part is slow you should look into some method to pre-process your vertex data (display lists or the modern equivalent) to take some of the weight of the render function.

  • Opengl get slow if lot of data and drawing but no need for too much, wont fit on monitor anyway.

    Then will start to learn threading for worker part ,

    Gui can be main thread but nice if gl also seperate then gui not connected to gl fps drops.

  • Lifetime Qt Champion

    @Rondog is right about what I meant.

    QWidget can not be manipulated outside the GUI thread.

    In what case are you experiencing OpenGL slow-down ? Before trying to put everything in different threads you should analyze what you are doing in OpenGL, how are you feeding data to the GPU etc.

  • Data to gpu is from ram generated to triangles , 2 triangles 1 line .
    If over 50 000 lines cpu get slow feeding and calculating for gpu , also ui refreshg rate drops but it no problem in normal operation .
    By taking calculating part away to seperate thread probably good enough to lose problems with gui refresh.

    If have time please answer few threading questions.

    If have created thread and try start it in main function by
    Calc_thread t_obj;
    Is should have ran as with while(1) loop cpu rised.

    this is thread .h file
    #ifndef CALC_THREAD_H
    #define CALC_THREAD_H

    #include <QtCore>

    class Calc_thread : public QThread

       void run();


    class Calc_Class
    void graph_code_run();

    How could voids in Calc_Class be accessed on seperate thread if it is created once in main function?
    Or have to put all functions under Calc_thread class?

  • Lifetime Qt Champion

    Are you sure you're doing the jobs at the right places ? Generating everything with your CPU and then just use the GPU to show the result isn't the most efficient way to do things

  • @SGaist Data comes from vector containers that are loaded from disk, not sure how to make program more efficient.
    Not sure how to acess other class under other thread, should it be named to same class as thread to acess it?

  • Lifetime Qt Champion

    Do you read them once from the disk ?

  • @SGaist Yes , Once to ram to vectors further from ram.

  • Lifetime Qt Champion

    What do you mean by "to vectors further from ram" ?

  • @SGaist
    From disk is loaded to vector containers in ram , then further from vectors for graph generation or for testing that takes calculating power.
    Now it running seperate thread and in comparison to runnignsame thread as gui and opengl about 5% faster without forced gui refresh.
    No need put opengl seperate thread , it works well together with gui thread.

Log in to reply