Skip to content

Instantly share code, notes, and snippets.

@lpranam
Created November 24, 2018 15:23
Show Gist options
  • Save lpranam/1b49be1e925bb1575d44a1211d31e247 to your computer and use it in GitHub Desktop.
Save lpranam/1b49be1e925bb1575d44a1211d31e247 to your computer and use it in GitHub Desktop.
This gist is created for Google Code-In 2018 task "Tutorial 3 - Stopwatch using threading" of KDE.(https://codein.withgoogle.com/dashboard/tasks/4887904280117248/)

Threading is the main concept which every programmer needs to know to write a good program so in this tutorial we will build a stopwatch using threading. Please remember that this tutorial is meant to teach you the concept of threading and not how to build the stopwatch. Actual watches are made in a more efficient way than this.

Overview of task: The stopwatch will display hour, minutes and seconds. It will have a start and a stop push button. Once the clock is stopped and started again it will count from the beginning.

Prerequisites: Basic knowledge of QT class QThread

Steps:

  1. Create a new Qt GUI project with dialog.
  2. Create a layout for our clock. You can use LCD numbers widgets to make it look like a real watch. You will require 3 LCD number widget for hours, minutes and seconds respectively. Two push buttons one for start and one for stop.
  3. Now create a simple new C++ class with Qthread inherited.
  4. In this class, we will a constructor with two arguments QObject *parent and int count. In addition a private data member count and a signal countChanges(int). We will use this count variable to increment our time.
  5. In the constructor of the Counter class use member initialiser Qthread(parent) and also initialize our counter variable.
  6. Now we will override the method run() of QThread which is inherited. Inside it, we will have a simple for loop. for loop will start counting from 0 we will increment it by one and will not have any condition(will become infinite loop). Inside this loop, we will emit countChanges(i) signal. Then we will call sleep(count), this is also inside loop.
  7. Now inside our dialog class, we will have 3 pointer object of Counter class hour, min and sec. Initiate these objects with the parent object and the frequency of update in second(for hour update frequency will be 3600, for minute 60 and for second 1).
  8. Now connect SIGNAL countChanges with SLOT display() of LCD widget for all three Counter objects to its corresponding LCD widget.
  9. Create slot for start button clicked. Inside it call the start() method for each counter object.
  10. Create slot for stop button clicked. Inside it call the terminate() method for each counter object.

Submission:

  • Link to your repository
  • Video of your working stopwatch.

Try some experiment: See what happens if we change sleep() method with wait().

If you can not understand any part or solve any problem contact mentor they are always happy to help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment