Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

[180511] timer based on QTime class and QTimer class

Shulou Source: shulou.com Published: 2022-06-03 05:33:30 11月24日 Update

timer

First of all, the above picture

The interface construction is mainly lcdnumber and textbrowser.

First, the timing function is implemented, using the QTime class.

QTime class

Basic function

QTime (int h, int m, int s = 0, int ms = 0) / / constructor

QTime addMSecs (int ms) const / / A pair of time changes, of course, there is also the corresponding addSec function int hour () const / / to get the hours of the current time, similarly, minute,second,msecint elapsed () const / / returns the elapsed time from pause to restart int restart () and void start () / / restart and start, note that restart has a return value of elapsed time int msecsTo (const QTime & t) const / / compares the time difference between the two Qtime classes

Static member

QTime currentTime () / / get the current time QTimer class (timer)

Basic function

Void start (int msec) / / how many milliseconds to send a signal void stop () / / stop sending a signal

Signal

Void timeout () / / send this signal when the rest time is over

Use

QTimer * timer = new QTimer (this); connect (timer, SIGNAL (timeout ()), this, SLOT (update (); / / update () is the function you want to call, of course, it is not necessary for this to accept timer- > start (1000).

In fact, repeated updates to the window are maintained by calling the exec () function in Qtimer. But repeated updates bring a huge amount of work, which is expected to be solved in a threaded way (we'll talk about it later).

Timers are divided into precise and rough types, but no matter which kind of timer depends on its operating system and hardware performance. If the system is too busy to guarantee the accuracy of all requests of the timer, then no matter which kind of timer it is, there will be a deviation.

Introduction to the window LCDnumber: it needs to be emphasized that it cannot get the displayed text, and if you do need text, you can connect its display () function to a slot function. Can be used to display Qstringtextbrowser: rich text edit box, read-only but not editable. The textedit class does not have the implementation of the rich text editing function to click the start button, it needs to be able to start timing. So I create a Qtime object in the constructor of Mainwindow to record the current time of the computer at the moment the start key is pressed. When I press pause or stop to create another Qtime object, the two objects compare the difference, the difference is the recording time. Because of my LCD real-time display, I'm going to initialize the LCD at the beginning.

This- > ui- > lcdNumber- > display ("00VlV 0000VOG 00000")

Then write an object of Qtimer in the Mainwindow constructor and let LCD update the time every millisecond. Change the bug on the button: for example, you can't pause when you press stop. Only appendText () (append) is needed to display the dotted data to textbrowser. When stopping, there should be a clear key to clear all dotted records and set the LCD to zero when the pause time should be removed.

Tags: Time function timer signal object text difference update two function foundation system huge precise rough operating system image above task deviation accuracy Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Tech Info Linux Shulou Information Redmi