Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. QTimer *timer = new QTimer;
  2. timer->start(10);
  3. connect(timer,SIGNAL(timeout()),someObject,SLOT(someFunction()));
  4.  
  5. QApplication app(argc, argv);
  6. [...]
  7. app.exec();
  8.  
  9. while(1)
  10. {
  11. SleepUntilThereIsSomethingToDo(); // not a real function name!
  12. DoTheThingsThatNeedDoingNow(); // this is also a name I made up
  13. if (timeToQuit) break;
  14. }
  15.  
  16. int select(int nfds,
  17. fd_set *readfds,
  18. fd_set *writefds,
  19. fd_set *exceptfds,
  20. struct timeval *timeout);
  21.  
  22. void SleepUntilThereIsSomethingToDo()
  23. {
  24. struct timeval now = gettimeofday(); // get the current time
  25. struct timeval nextQTimerTime = [...]; // time at which we want to emit a timeout() signal, as was calculated earlier inside QTimer::start()
  26. struct timeval maxSleepTimeInterval = (nextQTimerTime-now);
  27. select([...], &maxSleepTimeInterval); // sleep until the appointed time (or until I/O arrives, whichever comes first)
  28. }
  29.  
  30. void DoTheThingsThatNeedDoingNow()
  31. {
  32. // Is it time to emit the timeout() signal yet?
  33. struct timeval now = gettimeofday();
  34. if (now >= nextQTimerTime) emit timeout();
  35.  
  36. [... do any other stuff that might need doing as well ...]
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement