29 First Knowledge Thread 2

Source: Internet
Author: User
Tags emit

The previous section describes the creation of threads, a single thread as a class and an inheritance Qthread, and only one thread handler function. A big drawback.

    1. Specifies the inheritance Qthread class, to use other base classes such as Qwidget?
    2. The thread handler function run () can only be overridden and cannot be customized.

Therefore, the following working methods are recommended for Qt4.7 and later versions. Its main feature is to take advantage of the event-driven nature of QT, placing the business that needs to be handled in a child thread in a separate module (class), after the main thread has created the object, handing it over to the specified thread, and can hand over multiple similar objects to the same thread.

In other words, you create a new class Mythread, which is not inheriting the Qthread class, but must inherit qobject. Consider mythread as a normal class non-threading class. The Qthread class is then used in the form to create the child thread object, attaching the class mythread to the child thread qthread. (Mythread is a passenger, Qthread is a car).

Question one: Why can't a custom class specify a parent object?

void Qobject::movetothread (Qthread * targetthread)

In call Movetothread, Movetothread is not allowed if the custom class specifies a parent object, and Qthread is a parent object.

Question two: Why use Signal-slot instead of calling the threading function directly.

A direct call causes the threading function and the main thread to be in the same thread.

Newthread->myslot ()

Using Signal-slot to invoke, the main thread and the child line will be independent.

Considerations during Multithreading:

? A thread cannot manipulate a UI object (a Window object derived directly or indirectly from Qwidget)

? You cannot specify a parent object when you create an object that needs to be moved to a module class that is processed in a child thread.

Result diagram:

Source:

Custom Threads Section

Newthread.h

#ifndef Newthread_h

#define Newthread_h

#include <QObject>

Class Newthread:public Qobject

{

Q_object

Public

Explicit Newthread (Qobject *parent = 0);

Public

void Myslot (); Custom threading Functions

Signals:

void Mysignal ();//Custom signal

Public Slots:

};

#endif//Newthread_h

Newthread.cpp

#include "Newthread.h"

#include <QThread>

#include <QDebug>

Newthread::newthread (Qobject *parent):

Qobject (parent)

{

}

void Newthread::myslot ()

{

Qthread::sleep (5);

Operation completed, Signal sent

Emit mysignal ();

Qdebug () << "Child thread ID:" <<qthread::currentthreadid ();

}

Main thread part

Widget.h

#ifndef Widget_h

#define Widget_h

#include <QWidget>

#include <QTimer>

#include "Newthread.h"

#include <QThread>

Namespace Ui {

Class Widget;

}

Class Widget:public Qwidget

{

Q_object

Public

Explicit Widget (Qwidget *parent = 0);

~widget ();

Signals:

void Threadsignal (); Prompt to invoke thread handler function

Private Slots:

void on_pushbutton_clicked (); Start button

void on_pushbutton_2_clicked (); Pause button

Private

Ui::widget *ui;

Qtimer *timer; Timer

Newthread *mythread; Custom Thread Objects

Qthread *thread; Child threads

};

#endif//Widget_h

Widget.cpp

#include "Widget.h"

#include "Ui_widget.h"

#include <QThread>

#include <QDebug>

Widget::widget (Qwidget *parent):

Qwidget (parent),

UI (New Ui::widget)

{

UI->SETUPUI (this);

Custom Thread object, no parent object can be specified

MyThread =new Newthread;

Creating Child Threads

Thread =new qthread (this);

Attaching a custom thread object to a child thread

Mythread->movetothread (thread);

Timer=new Qtimer (this);

Timer signal

Connect (Timer,&qtimer::timeout,

[=] ()

{

static int num=0;

Ui->lcdnumber->display (num);

num++;

}

);

Calling a thread handler function

Connect (This,&widget::threadsignal,mythread,&newthread::myslot);

Accept a sub-thread to send a signal

Connect (mythread,&newthread::mysignal,

[=] ()

{

Qdebug () << "over";

Timer->stop ();

}

);

Connect (this,&qwidget::d estroyed,

[=] ()

{

Thread->quit ();

Thread->wait ();

}

);

}

Widget::~widget ()

{

Delete UI;

}

void Widget::on_pushbutton_clicked ()

{

Start timer

if (timer->isactive ()!=true)

Timer->start (1000);

Starts a thread, but does not start a thread-handling function

Thread->start ();

Send a signal to call the thread handler function

Emit threadsignal ();

Mythread->myslot ();

Qdebug () << "main thread ID:" <<qthread::currentthreadid ();

}

void Widget::on_pushbutton_2_clicked ()

{

if (timer->isactive () ==true)

Timer->stop ();

}

Add:

For the last parameter of the Connect function of the Qobject class, the connection type:

? Automatic connection (autoconnection), the default mode of connection.

? If the signal and the slot, that is, the sender and the recipient in the same thread, the equivalent of a direct connection;

? If the sender is in a different thread than the recipient, it is the same as a queue connection.

? Direct connection (directconnection)

When the signal is emitted, the slot function is immediately called directly. The slot function is always executed by the sender, regardless of which thread the slot function belongs to.

? Queue Connection (queuedconnection)

The slot function is called when control returns to the event loop of the recipient's thread. The slot function executes on the thread of the recipient.

Summarize:

* Queue connection: The slot function executes on the thread of the recipient.

* Direct connection: The slot function executes on the sender's thread.

* Automatic connection: When they are not on the same thread, they are identical to the queue connection

29 First Knowledge Thread 2

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.