simpletimerqt.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "simpletimerqt.h"
  2. #include "ui_simpletimerqt.h"
  3. SimpleTimerQt::SimpleTimerQt(QWidget *parent)
  4. : QMainWindow(parent)
  5. , ui(new Ui::SimpleTimerQt)
  6. {
  7. ui->setupUi(this);
  8. timer = new QTimer(this);
  9. connect(timer, SIGNAL(timeout()), this, SLOT(timer_slot()));
  10. m = s = ms = 1;
  11. time = "%1:%2:%3";
  12. }
  13. SimpleTimerQt::~SimpleTimerQt()
  14. {
  15. delete ui;
  16. }
  17. void SimpleTimerQt::timer_slot()
  18. {
  19. if(m != 0 || s != 0 || ms != 0)
  20. {
  21. ms--;
  22. if(ms < 0)
  23. {
  24. ms = 99;
  25. s--;
  26. if(s < 0)
  27. {
  28. s = 59;
  29. if(m > 0)
  30. m--;
  31. }
  32. }
  33. }
  34. else
  35. {
  36. timer->stop();
  37. ui->pushButtonSetValues->show();
  38. flagbtn = !flagbtn;
  39. ui->pushButton->setText("Start");
  40. }
  41. ui->timeLabel->setText(time.arg(m, 3, 10, QLatin1Char(' ')).arg(s, 2, 10, QLatin1Char('0')).arg(ms, 2, 10, QLatin1Char('0')));
  42. }
  43. void SimpleTimerQt::on_pushButton_clicked()
  44. {
  45. flagbtn = !flagbtn;
  46. if(flagbtn)
  47. {
  48. ui->pushButtonSetValues->show();
  49. ui->pushButton->setText("Start");
  50. timer->stop();
  51. }
  52. else
  53. {
  54. ui->pushButtonSetValues->hide();
  55. ui->pushButton->setText("Stop");
  56. timer->start(10);
  57. }
  58. }
  59. void SimpleTimerQt::on_pushButtonSetValues_clicked()
  60. {
  61. m = ui->spinBoxMinutes->value();
  62. s = ui->spinBoxSeconds->value();
  63. ms = ui->spinBoxMS->value();
  64. ui->timeLabel->setText(time.arg(m, 3, 10, QLatin1Char(' ')).arg(s, 2, 10, QLatin1Char('0')).arg(ms, 2, 10, QLatin1Char('0')));
  65. }