How do I create a pause/wait function using Qt?

后端 未结 12 2201
执笔经年
执笔经年 2020-11-27 15:19

I\'m playing around with Qt, and I want to create a simple pause between two commands. However it won\'t seem to let me use Sleep(int mili);, and I can\'t find

12条回答
  •  爱一瞬间的悲伤
    2020-11-27 16:16

    @kshark27's answer didn't work for me for some reason (because I use Qt 5.7?) so I ended up doing this:

    while (someCondition) {
    
       // do something
    
       QApplication::processEvents();
       QThread::sleep(1); // 1 second
    
    };
    

    If this is done in the GUI thread, it obviously introduces a 1 second GUI lag before responding to user events. But if you can live with it, this solution is probably an easiest to implement and even Qt endorses it in their Threading Basics article (see When to Use Alternatives to Threads section).

提交回复
热议问题