QT Development (v)-Project combat: Stopwatch, the use of Qtime,qtimer

Source: Internet
Author: User

We continue the journey of QT, this time is still a small thing, it is a stopwatch, this stopwatch is mainly logical operation, in fact, there is not much content, that is, learning about the use of qtime and Qtimer, we look at what the UI looks like

Here is very simple, we first analyze the control, the top is a qlcdnumber to display the number of seconds, the middle of four buttons to control, the following is a record point of logic, control using Qtextbrowser, OK, let's look at the bottom of the file

#ifndef mainwindow_h
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTime>
# Include <QTimer>

namespace Ui {
class MainWindow;
}

Class Mainwindow:public Qmainwindow
{
    q_object public

:
    explicit MainWindow (Qwidget *parent = 0); C12/>~mainwindow ();

Public:
    void init ();

Private slots:

    void Updatedisplay ();

    void on_btn_start_clicked ();

    void on_btn_stop_clicked ();

    void on_btn_pause_clicked ();

    void on_btn_point_clicked ();

Private:
    Ui::mainwindow *ui;

    Qtimer *ptimer;
    Qtime Basetime;
    The time displayed
    QString timestr;

#endif//Mainwindow_h

As you can see, I've defined a common function. init is used to initialize some data, five slot functions, respectively, four buttons and a slot function with a countdown, and define some variables, where timestr is the final text displayed on the LCD, okay, let's start the analysis.

Initialize
void Mainwindow::init ()
{
    ///default display
    This->ui->lcd_play->display ("00:00:00:000");

    This->ptimer = new Qtimer;
    Bind timer signal
    Connect (this->ptimer,signal (timeout ()), This,slot (Updatedisplay ()));

The first is that we define the format that needs to be displayed in the initialization as 00:00:00:000, because we can only do this by code, and then we define a timer with a signal of timeout.

Start
void mainwindow::on_btn_start_clicked ()
{
    //Get current time
    this->basetime = this-> Basetime.currenttime ();
    This->ptimer->start (1);

    Reset State
    if (This->ui->btn_stop->text ()!= "Stop")
    {
        This->ui->btn_stop->settext ("Stop ");
    }
    if (This->ui->btn_pause->text ()!= "paused")
    {
        this->ui->btn_pause->settext ("paused");
    }

Then we click on the start slot function, we get the current time and set it on the Basetime, and reset the other button's state

End
void mainwindow::on_btn_stop_clicked ()
{
    if (this->ui->btn_stop->text () = "Stop")
    {
        this->ui->btn_stop->settext ("Empty");
        This->ptimer->stop ();
    }
    else if (this->ui->btn_stop->text () = "Empty")
    {
        this->ui->btn_stop->settext ("Stop");
        This->ui->tb_display->clear ();
        This->ui->lcd_play->display ("00:00:00:000");
    }

We have two states at the end, stop and call the stop on the timer pointer, and then restore the state when we clear it.

Pause
void mainwindow::on_btn_pause_clicked ()
{
    static qtime pausetime;
    if (this->ui->btn_pause->text () = = "Paused")
    {
        pausetime = Qtime::currenttime ();
        This->ui->btn_pause->settext ("continue");
        This->ptimer->stop ();
    }
    else if (this->ui->btn_pause->text () = = "Continue")
    {
        //Perform difference calculation
        qtime cut = Qtime::currenttime ();
        int t = Pausetime.msecsto (cut);
        This->basetime = This->basetime.addmsecs (t);
        This->ui->btn_pause->settext ("suspend");
        This->ptimer->start ();
    }

We're pausing here, first defining a pausetime to get the time you clicked, and then calling stop, and if you press on again, I'll compare my saved pausetime with the last basetime and add it up to make up for the difference. Finally call the Start function

Manage
void mainwindow::on_btn_point_clicked ()
{
    //Add TIMESTR to the list
    this->ui->tb_display- >append (THIS->TIMESTR);
}

It's easier to manage, just add it up.

Update time in corresponding slot function

Update time
void Mainwindow::updatedisplay ()
{
    /
     * * 1. Click Start to get to the current time and assign value to Basetime
     * 2. Start timer, interval 1s
     * 3. Slot function to get the current time currtime
     * 4. Calculates the difference of two time t
     * 5. Declare a Showtime object to give him the difference of T
     (* 6). Format display
    after formatting Qtime currtime = Qtime::currenttime ();
    int t = This->basetime.msecsto (currtime);
    Qtime ShowTime (0,0,0,0);
    ShowTime = Showtime.addmsecs (t);
    This->timestr = showtime.tostring ("hh:mm:ss:zzz");
    This->ui->lcd_play->display (TIMESTR);
}

It's the same way to make up for the difference.

Okay, let's take a look at all the code.

#include "mainwindow.h" #include "ui_mainwindow.h" #include <QString> mainwindow::mainwindow (qwidget *parent):
    Qmainwindow (parent), UI (new Ui::mainwindow) {ui->setupui (this);
This->init ();
    } mainwindow::~mainwindow () {Delete this->ptimer;
Delete UI;

    }//Initialize void Mainwindow::init () {///default display This->ui->lcd_play->display ("00:00:00:000");
    This->ptimer = new Qtimer;
Bind timer signal Connect (this->ptimer,signal (timeout ()), This,slot (Updatedisplay ()));
    }//Start void mainwindow::on_btn_start_clicked () {//get current time This->basetime = This->basetime.currenttime ();

    This->ptimer->start (1);
    Reset state if (This->ui->btn_stop->text ()!= "Stop") {This->ui->btn_stop->settext ("Stop");
    } if (This->ui->btn_pause->text ()!= "paused") {This->ui->btn_pause->settext ("paused"); }//End void mainwindow::on_btn_stop_clicked () {if (This->ui->btn_stoP->text () = = "Stop") {This->ui->btn_stop->settext ("empty");
    This->ptimer->stop ();
        else if (this->ui->btn_stop->text () = "Empty") {This->ui->btn_stop->settext ("Stop");
        This->ui->tb_display->clear ();
    This->ui->lcd_play->display ("00:00:00:000");
    }//Pause void mainwindow::on_btn_pause_clicked () {static qtime pausetime;
        if (this->ui->btn_pause->text () = = "Paused") {Pausetime = Qtime::currenttime ();
        This->ui->btn_pause->settext ("continue");
    This->ptimer->stop (); else if (this->ui->btn_pause->text () = "Continue") {//Perform difference calculation qtime cut = Qtime::currenttime (
        );
        int t = Pausetime.msecsto (cut);
        This->basetime = This->basetime.addmsecs (t);
        This->ui->btn_pause->settext ("suspend");
    This->ptimer->start (); }//mainwindow::on_btn_point_clicked void () {//Add Timestr to the list this->ui->tb_display->append (THIS-&GT;TIMESTR); }//Update time void Mainwindow::updatedisplay () {/* * 1. Click Start to get to the current time and assign value to Basetime * 2. Start timer, interval 1s * 3. Slot function again Get Current Time Currtime * 4. Calculates the difference of two time t * 5. Declare a Showtime object and give him the difference of T (6). format to display * * Qtime currtime = qtime::
    CurrentTime ();
    int t = This->basetime.msecsto (currtime);
    Qtime ShowTime (0,0,0,0);
    ShowTime = Showtime.addmsecs (t);
    This->timestr = showtime.tostring ("hh:mm:ss:zzz");
This->ui->lcd_play->display (TIMESTR);
 }

And look at the effect.

OK, finally send the source code: Click to download interested can add Group: 690351511

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.