(E) TimerΒΆ
Goal:
I will learn to use QTimer
objects and timeout
signals that will be
useful in the GUI project on the next round
(if you will implement extra features).
In addition, I will practise setting a background color for a widget.
Instructions:
Retrieve the code template: templates/12/timer/
-> student/12/timer/
.
The task is to implement the following kind of dialog with Qt Designer.
In the template code, there is already a pointer to a QTimer
object.
Comparing to the previous exercise Find dialog, this exercise has only one new widget: LCD number for showing the minutes and seconds. In addition, you need read Qt documentation to find out how to set colors for widgets. (You can choose other colors than shown in the figure above.)
The buttons works as follows.
Start
button starts the timer. It starts from those minutes and seconds that are there on the display at the moment. (If the timer is on, clicking the button has no effect.)Stop
button stops the timer, whereupon the current minutes and seconds remain on the display. (If the timer has already been stopped, clicking the button has no effect.)Reset
button resets the timer, whereupon both minutes and seconds are set to zero.Close
button closes the window.
Attention
Use the following object names: lcdNumberSec
, lcdNumberMin
,
startButton
, stopButton
, resetButton
, and closeButton
.
In the same way as in the Find dialog exercise, use the signal & slot mechanism to connect suitable actions for the buttons.
In addition, you need a QTimer
object that can emit timeout
signals
at regular time intervals.
For example, if the time interval is 1000 milliseconds
(i.e. 1 second), the timeout
signal will be emitted every second.
Connect timeout
signal to the slot processing it, and write necessary
processing code.
Now you have to do the connection in the code, a suitable place for it is
the constructor of MainWindow
.
Tips for completing the assignment:
- A good place to start is to construct the user interface.
- LCD number widget has the method
display
that you can use to show the seconds and minutes. - For debug prints you can use the library
QDebug
. - Instructions on how set a background color can be found from Qt documentation.
- Initialize minutes and seconds with zeros.
A+ presents the exercise submission form here.