What is the alternative to QTimer?

巧了我就是萌 提交于 2021-02-10 15:06:03

问题


The slot function QTimer::start have a interval of millisecond:

Starts or restarts the timer with a timeout interval of msec milliseconds.

But msec is a int type and couldn't take a long interval. What is a alternative to QTimer which could take a long interval ?


回答1:


In this case you can use something like an hourly timer connected to a function or lambda that checks the current timepoint against a target...

/*
 * We want to trigger some event one year from now.
 */
auto endpoint = QDateTime::now().addYears(1);
QTimer hourly_timer;
QObject::connect(&hourly_timer, &QTimer::timeout,
                 [endpoint]()
                   {
                     if (QDateTime::now() >= endpoint) {

                       /*
                        * Target time reached.  Do whatever...
                        */
                     }
                   });
hourly_timer.start(3600 * 1000);


来源:https://stackoverflow.com/questions/62481663/what-is-the-alternative-to-qtimer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!