Important: Please read the Qt Code of Conduct -

How to make a stopwatch in C++/Qt

  • Hello everyone,

    I'm new to this forum and Qt and C++ in general. So I don't have much programming experience.
    I'm trying to make a little Qt application (on Linux, for Linux, Fedora specifically) that can record times.
    And save them in XML files. Each person will have a separate XML file for his times. Also I want to build in
    an option to read the XML files and view the times of the person later on. Well I think the first step is making
    a simple stopwatch application. Does anyone know how to? I have already read articles about QTimer and Ctime.
    But I just don't understand them.

    Also it would be nice if someone could tell me which XML library (for reading and writing) is the best for a real beginner. I already been searching with Google. But I just couldn't choose. There are so many libraries. Library must be open source.
    Everyone tells me to use a different one. Which is the best? It doesn't have to be really fast as I will only be reading little XML files.

    Kind Regards,


  • If you don't understand article about QTimer you really should read more about Qt and/or C++ (do you understand signals and slots, basics of OOP?).

    There is a module in Qt for Xml so it's unlikely that you will need some other library for your project.

  • Thanks p-himik. I do understand signal and slots, I think. A signal is given when an particulary event occurs, for example when a button is pressed and a slot is a piece of code that will be executed when that signal which it' s connected to is given/fired. Is that true?

    Well I don't think I understand Object Oriented Programming. And would you like to tell me what' s the name of that Qt/XML module, please?

    Kind Regards,


    EDIT: In the meantime I have found this and with that information I have written
    this code:

    #include <time.h>
    #include <stdio.h>

    int totalTime=0;

    int main {
    time_t start,end;

    if(std::cin >> "start") {
    if(std::cin >> "stop") {
    totalTime = end-start;
    std::cout << totalTime

    But I GCC gave these errors:

    [compiler output]
    /home/Superpelican/Documents/Programming_Projects/stopwatch.cpp:6:5: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]
    /home/Superpelican/Documents/Programming_Projects/stopwatch.cpp:7:8: error: expected primary-expression before ‘start’
    /home/Superpelican/Documents/Programming_Projects/stopwatch.cpp:7:8: error: expected ‘}’ before ‘start’
    /home/Superpelican/Documents/Programming_Projects/stopwatch.cpp:7:8: error: expected ‘,’ or ‘;’ before ‘start’
    /home/Superpelican/Documents/Programming_Projects/stopwatch.cpp:9:1: error: expected unqualified-id before ‘if’
    /home/Superpelican/Documents/Programming_Projects/stopwatch.cpp:12:1: error: expected unqualified-id before ‘if’
    /home/Superpelican/Documents/Programming_Projects/stopwatch.cpp:15:1: error: ‘totalTime’ does not name a type
    /home/Superpelican/Documents/Programming_Projects/stopwatch.cpp:16:1: error: ‘cout’ in namespace ‘std’ does not name a type
    /home/Superpelican/Documents/Programming_Projects/stopwatch.cpp:17:1: error: expected declaration before ‘}’ token
    [/compiler output]

  • Superpelican, you really have to read some books about C++ because your code shows significant lack of knowledge in C++ (and C).

  • Thanks, I'm going to buy a good C++ book for beginners.

  • Consider these also :)

Log in to reply