timeupdatethread.cpp 795 B

12345678910111213141516171819202122232425262728
  1. #include "timeupdatethread.h"
  2. TimeUpdateThread::TimeUpdateThread(QObject *parent) : QThread(parent) {
  3. timer = new QTimer(this);
  4. connect(timer, &QTimer::timeout, [this]() {
  5. QDateTime currentDateTime = QDateTime::currentDateTime();
  6. QString currentTimeString = currentDateTime.toString("yyyy-MM-dd hh:mm:ss");
  7. emit timeUpdated(currentTimeString);
  8. });
  9. }
  10. void TimeUpdateThread::stop() {
  11. isStopped = true;
  12. timer->stop();
  13. quit();
  14. }
  15. TimeUpdateThread::~TimeUpdateThread() {
  16. stop();
  17. wait();
  18. }
  19. void TimeUpdateThread::run() {
  20. timer->start(1000);
  21. QDateTime currentDateTime = QDateTime::currentDateTime();
  22. QString currentTimeString = currentDateTime.toString("yyyy-MM-dd hh:mm:ss");
  23. emit timeUpdated(currentTimeString);
  24. exec();
  25. }