Interface threads vs. worker threads
The main thread of the GUI program:
GUI user input is mainly from mouse, keyboard, called event.
The main loop that handles the event, called the event loop. This thread, called the interface thread, is used to process various interfaces.
GUI interface card dead
All event handlers (slots) should be returned quickly, otherwise the main interface will die
For example: When the mouse clicks a button, this handler function should be returned as soon as possible. During its processing, the interface is stuck. (Because this function is in the event loop)
Workaround: Put the long-running task on another thread (worker thread) to complete and maintain the active state of the interface.
Second, timers (timer)
Timers are used to implement timed operations such as:
Automatically saved every 3 seconds
Blinks once every 500 milliseconds
Refresh display every 1 seconds interface
How to use:
1: rewrite virtual void timerevent (qtimerevent* event);
2: Start timer, specify time interval (milliseconds)
M_timerld = Starttimer (500);
Small exercise:
Implements a text clock that displays the current time of the system
Class Test8_1a_12_8:public qmainwindow{q_objectpublic:test8_1a_12_8 (qwidget *parent = q_nullptr); virtual void Ti Merevent (qtimerevent* event);//override the function (inherited from Qobject) Private:ui::test8_1a_12_8class Ui;int m_timerid;//Each timer has an ID}; Test8_1a_12_8::test8_1a_12_8 (Qwidget *parent): Qmainwindow (parent) {UI.SETUPUI (this); M_timerid = Starttimer (500);} void Test8_1a_12_8::timerevent (qtimerevent* event) {//can have multiple timers, each timer has a different processing if (event->timerid () = = M_timerid) { Qtime now = Qtime::currenttime (); QString Text = now.tostring ("HH:mm:ss"); Ui.lineedittime->settext (text);}}
Effect:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/8B/52/wKiom1hJYACz0R8aAAAH9RdbKrE278.png "title=" 1.png " alt= "Wkiom1hjyacz0r8aaaah9rdbkre278.png"/>
For timers, pay attention to the following:
1, the timer is not a thread: timer time and ordinary mouse, keyboard events, into the event loop processing, therefore, the timer processing function needs to be completed as soon as possible, otherwise it will also put the interface card dead;
2, the timer is not accurate, because it needs to be placed in the event loop processing, so if other events occupy more events, will cause the timer is not accurate
Third, create the thread
In Qt, thread-related classes:
Qthread: Thread
Qmutex: Mutual exclusion lock
Qsemaphore: Signal Volume
Methods for creating Threads:
1. Derive a thread class, inherit from Qthread, and then rewrite the entry function run () method
Start: Start ()
Recycle: Wait ()
2. Add some member functions to query the status and progress of the task
Status: Int GetStatus (); return value: 1 (completed) 2 (in progress) 3 (error, terminated)
Progress: int getprogress (); return value: 0 ~ 100
To create a worker thread:
A worker thread is a normal thread, different from the "interface thread"
When an event processing may take a long time, create a thread to complete it, lest the thread die.
Steps:
1. Create and start a worker thread
2. Display a progress bar or wait dialog box
3, start a timer, check the progress and status of the worker thread, after its completion, fill the progress bar, end waiting
Small exercise:
Simulate the transfer of a file to show the progress of a task
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/8B/5B/wKiom1hKUwbgel-NAAARM9paDYg084.png "style=" float: none; "title=" 2.png "alt=" Wkiom1hkuwbgel-naaarm9padyg084.png "/>
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/8B/58/wKioL1hKUwbiGrALAAAN_DEEEAQ755.png "style=" float: none; "title=" 3.png "alt=" Wkiol1hkuwbigralaaan_deeeaq755.png "/>
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/8B/58/wKioL1hKUwei2IhhAAALGkrwJOE881.png "style=" float: none; "title=" 4.png "alt=" Wkiol1hkuwei2ihhaaalgkrwjoe881.png "/>
Code:
GitHub
Https://github.com/HonestFox/Qt/tree/master/12_8FileTransferStatu
Multithreading in the QT learning notes 8.Qt