-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
107 lines (102 loc) · 2.79 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <iostream>
#include <QTimer>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow),
minutes(0),
seconds(0)
{
ui->setupUi(this);
setWindowTitle("Timer");
connect(ui->startButton, &QPushButton::clicked, this, &MainWindow::on_pushButton_clicked);
qtmer_p = new QTimer(this);
qtmer_p -> setInterval(1000);
// connect timer and timeout slot
connect(qtmer_p, &QTimer::timeout, this, &MainWindow::time_out);
// add about button
QAction* about = ui->menubar ->addAction("About");
connect(about, &QAction::triggered, [=](){
QMessageBox::aboutQt(ui->menubar, "AboutQt");
});
connect(this, &MainWindow::doIt, ui->menuSettings, &SettingMenu::timeOver);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
std::cout << "Runnded." << std::endl;
// this variable let the function knows if the user want to start
// counter or not.
static bool status = true;
if(status)
{
// get the user minute
u_m = ui->minNum->value();
if(!u_m)
{
;
}
else
{
// reset class minute
minutes = 0;
ui->minNum->setValue(0);
// start counter
qtmer_p->start();
// change button
ui->startButton->setText("Stop!");
// make spin box not avalibale to user
ui->minNum->setEnabled(false);
ui->minNum->hide();
// change the prompt text
ui->prompt->setText("<h1>Please Wait...</h1>");
// hide the time unit
ui->timeUnit->hide();
status = false;
}
}
else
{
// stop counter
qtmer_p->stop();
// make a new counter
// reset time values
minutes = 0;
seconds = 0;
// reset button
ui->startButton->setText("Start!");
// reset spin box
ui->minNum->setValue(u_m);
ui->minNum->setEnabled(true);
ui->minNum->show();
ui->timeUnit->show();
ui->prompt->setText("<h2>How many minutes do you want to wait:</h2>");
status = true;
}
}
void MainWindow::time_out()
{
std::cout << "Time outed." << std::endl;
seconds += 1;
QString qs;
ui->lcdSecond->display(seconds);
ui->lcdMinute->display(minutes);
std::cout << "This is minute: " << minutes << std::endl;
std::cout << "This is second: " << seconds << std::endl;
if(seconds == 60)
{
minutes += 1;
seconds = 0;
}
if(minutes == u_m && u_m != 0)
{
emit MainWindow::doIt();
emit MainWindow::on_pushButton_clicked();
}
}