12345678910111213141516171819202122 |
- #include "timeupdatethread.h"
- #include <QTimer>
- TimeUpdateThread::TimeUpdateThread(QObject *parent) : QThread(parent) {}
- void TimeUpdateThread::run() {
- QTimer timer;
- connect(&timer, &QTimer::timeout, [this]() {
- QDateTime currentDateTime = QDateTime::currentDateTime();
- QString currentTimeString = currentDateTime.toString("yyyy-MM-dd hh:mm:ss");
- emit timeUpdated(currentTimeString);
- });
- timer.start(1000);
- // 初始化时立即更新一次时间
- QDateTime currentDateTime = QDateTime::currentDateTime();
- QString currentTimeString = currentDateTime.toString("yyyy-MM-dd hh:mm:ss");
- emit timeUpdated(currentTimeString);
- exec(); // 启动线程的事件循环
- }
|