Evgenii Legotckoi
Evgenii LegotckoiMay 9, 2016, 1:12 p.m.

Qt/C++ - Lesson 047. QThread – How to make threads in Qt

One of the most common ways to create a separate parallel threads in an application to Qt , and perform useful tasks in them is the inheritance of QThread class and override the method run() , which will be carried out and useful application code. When meeting with threads have read different opinions on this subject, and in this lesson will get acquainted with one of the possible ways to work with streams, but which are not considered to be the best option.

In general, if we make the class inherit from the QThread , it is logical to assume that this is done with the intention that it is not sufficient functionality of the class. But when this is done only with the aim to bring in, which must be performed in a separate thread run() method a useful code, then there is clearly something wrong. In addition, de might be a problem with scaling applications and reuse code, particularly hard this can be shown in the case where such inherited class becomes quite a lot.

This method is the low level, and is used primarily for customization native streams. What is somewhat contrary to the usual need to complete a task in a separate thread. That is, as mentioned above, such an approach is primarily needed to extend the functionality class. Nevertheless, this method is necessary to consider what and proceed.


main.cpp

This example is very small, so we will not go into the details of the project structure. Note that there will be created a console application in the file main.cpp which will create three streams with different names. A stream classes will be inherited from QThread .

#include <QCoreApplication>
#include "examplethreads.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    ExampleThreads threadA("thread A"); 
    ExampleThreads threadB("thread B"); 
    ExampleThreads threadC("thread C");

    threadA.start();    // Run threads
    threadB.start();    // and seeing their parallel operation
    threadC.start();    // in the output qDebug

    return a.exec();
}

examplethreads.h

#ifndef EXAMPLETHREADS_H
#define EXAMPLETHREADS_H

#include <QThread>

class ExampleThreads : public QThread
{
public:
    explicit ExampleThreads(QString threadName);

    // Override run () method, which will be located 
    // executable code
    void run();
private:
    QString name;   // thread name
};

#endif // EXAMPLETHREADS_H

examplethreads.cpp

#include "examplethreads.h"
#include <QDebug>

ExampleThreads::ExampleThreads(QString threadName) :
    name(threadName)
{

}

void ExampleThreads::run()
{
    for (int i = 0; i <= 100; i++ ) {
        qDebug() << name << " " << i;
    }
}

Output threads

Video

We recommend hosting TIMEWEB
We recommend hosting TIMEWEB
Stable hosting, on which the social network EVILEG is located. For projects on Django we recommend VDS hosting.

Do you like it? Share on social networks!

i
  • Nov. 11, 2024, 2:04 a.m.

order priligy online usa The type 2 diabetes statistics by state tiger was also very responsive, and suddenly avoided the python is first attack with a jump, but the python would naturally not give up when it suffered, and then frantically bit the other side again

Comments

Only authorized users can post comments.
Please, Log in or Sign up
AD

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:50points,
  • Rating points-4
m

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:80points,
  • Rating points4
m

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:20points,
  • Rating points-10
Last comments
i
innorwallNov. 14, 2024, 12:03 p.m.
How to make game using Qt - Lesson 3. Interaction with other objects what is priligy tablets What happens during the LASIK surgery process
i
innorwallNov. 14, 2024, 9:09 a.m.
Using variables declared in CMakeLists.txt inside C ++ files where can i buy priligy online safely Tom Platz How about things like we read about in the magazines like roid rage and does that really
i
innorwallNov. 12, 2024, 11:12 a.m.
Django - Tutorial 055. How to write auto populate field functionality Freckles because of several brand names retin a, atralin buy generic priligy
i
innorwallNov. 12, 2024, 7:23 a.m.
QML - Tutorial 035. Using enumerations in QML without C ++ priligy cvs 24 Together with antibiotics such as amphotericin B 10, griseofulvin 11 and streptomycin 12, chloramphenicol 9 is in the World Health Organisation s List of Essential Medici…
i
innorwallNov. 12, 2024, 4:50 a.m.
Qt/C++ - Lesson 052. Customization Qt Audio player in the style of AIMP It decreases stress, supports hormone balance, and regulates and increases blood flow to the reproductive organs buy priligy online safe Promising data were reported in a PDX model re…
Now discuss on the forum
i
innorwallNov. 14, 2024, 1:39 p.m.
добавить qlineseries в функции Listen intently to what Jerry says about Conditional Acceptance because that s the bargaining chip in the song and dance you will have to engage in to protect yourself and your family from AMI S…
i
innorwallNov. 11, 2024, 11:55 p.m.
Всё ещё разбираюсь с кешем. priligy walgreens levitra dulcolax carbs The third ring was found to be made up of ultra relativistic electrons, which are also present in both the outer and inner rings
9
9AnonimOct. 25, 2024, 9:10 p.m.
Машина тьюринга // Начальное состояние 0 0, ,<,1 // Переход в состояние 1 при пустом символе 0,0,>,0 // Остаемся в состоянии 0, двигаясь вправо при встрече 0 0,1,>…

Follow us in social networks