QT multithreading and communication via events (via custom events, then qapplication::p ostevent to the main interface, I used a signal slot before)

Source: Internet
Author: User

Cross-platform multithreaded development can be achieved through qthread, and the QT Library is responsible for specific multithreaded implementations on specific platforms. To use Qthread for multithreaded development, you first need to include a header file:

#include <QThread> then need to inherit a thread class from Qthread, Mythread, the key is to implement a virtual function run in Qthread. At the end of the run function, exec () is usually called, which causes the thread to enter the event loop, otherwise the thread's main function runs and the thread exits. Class Mythread:public Qthread{public:mythread ();p ublic:dialog *parent;public:void Run ();}; Where the parent stores the window that created the thread, the parent thread of that thread, which is used primarily to send specific events to the parent thread for inter-thread communication. The custom message first needs to include the header file: #include <QEvent> then define your own event values: const Qevent::type myevent = (qevent::type) 1234; And then inside the run function (in fact, in any place where you want to communicate with the parent thread, I'm simplifying here to be inside the Run function) by: qapplication::p ostevent (parent, New Qevent (MyEvent)); Send the event out. The parent thread is able to capture the message by overwriting the virtual function event: bool Dialog::event (qevent *event) {if (event->type () = = MyEvent) {Qmessagebox::inf      Ormation (This, "information", "This is son sented signal!");   return true; } return Qwidget::event (event);} In this way, two threads can run concurrently and be able to communicate between threads.

Http://www.cnblogs.com/bingcaihuang/archive/2011/03/16/1985971.html

Qt multithreading and communication via events (via custom events, then qapplication::p ostevent to the main interface, I used a signal slot)

Related Article

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.