The thread foundation of the QT Quick QuickStart

Source: Internet
Author: User
Tags emit

First of all, it must be clear that the use of the thread in Qt is relatively complex, not as random as in C #, especially the combination of serial, network programming, the use of a little careless will be problematic, and then QT often out of the problem on the direct collapse (this is really who use who know), So if the function of the asynchronous way can be done in fact there is no need to go to the thread, of course, if you write the function is more time-consuming (such as the continuous loop to do things like) can not avoid the use of threads.

Qt typically implements time-consuming operations in a single class, creates a class object in the main thread, and then moves the object to a child thread, where the main thread communicates with the child threads primarily through signals and slots. In addition, the object is created in the main thread, and the members of the object belong to the main thread, which can be accessed directly in the main and sub-threads, but if you move the members of the object into a child thread, the member is not directly accessible on the main thread.

Here is a usage instance.

 class  Threadmanager: public   qobject{q_object Qthread workerthread;  public     : Threadmanager ();     ~threadmanager (); Myworker  * Worker;signals:  void  Span style= "color: #000000;" > start ();  public   Slots:  void      Emitstart ();     void   Emitstop ();  void  printthreadid (int   
Threadmanager::threadmanager () {worker=NewMyworker (); Worker->movetothread (&workerthread); Connect ( This, SIGNAL (Start ()), Worker,slot (Start ())); Connect (worker,signal (Invokeparentthreadmethod (int)), This, SLOT (Printthreadid (int))); Workerthread.start ();} Threadmanager::~Threadmanager () {workerthread.quit ();}voidThreadmanager::emitstart () {emit start ();}voidThreadmanager::emitstop () {worker->stoped =true; Qdebug ()<<"Stop";}voidThreadmanager::p Rintthreadid (intcount) {    //worker->stoped = true;Qdebug () <<qthread::currentthreadid () <<"\ t"<<count;}

Threadmanager is used to open a new thread and then perform other operations on the thread, and the following is the class Myworker that performs the operation.

 class  myworker: public   qobject{q_object  public  :  explicit  myworker (qobject *parent = );  bool   stoped;signals:  void  Invokeparentthreadmethod ( count);  public   Slots:  void   Start ();};  
 Myworker::myworker (qobject *parent): Qobject (parent) {stoped  = false  ;}  void   Myworker::start () { int  Count =0  ;    stoped  = false  ;  while  (! <<qthread::currentthreadid () << " \t   <<count++;        Emit Invokeparentthreadmethod (count);    Qthread::msleep ( 500  ); }}


And look at how we use it in the main thread.

    qobject* win = Engine.rootobjects () [0];    Qobject* Button_start = win->findchild<qobject*> ("button_start");    Qobject* button_stop = win->findchild<qobject*> ("button_stop");    ThreadmanagerNew  Threadmanager ();    Qobject::connect (Button_start,signal (clicked ()), Therad,slot (Emitstart ()));    Qobject::connect (Button_stop,signal (clicked ()), Therad,slot (Emitstop () ));

Here is a simple example of threading, in the serial, network programming often involves thread synchronization, resource sharing, thread communication and other issues, such as the child thread is still using the main thread of resources to release the resources or the communication between the threads simultaneous the past data changes and so on, the specific situation only with their own experience to analyze.

The thread foundation of the QT Quick QuickStart

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.