Qt entry routine (2), qt entry routine

Source: Internet
Author: User

Qt entry routine (2), qt entry routine

This document uses the QtConcurrent: run () function in Qt as an example to describe how to run a function in a separate thread.

1 QtConcurrent: run ()

QtConcurrent is a namespace that provides high-level interface functions (APIs) to automatically adjust the number of running threads of a program based on the actual number of CPU cores in the current computer.

The following is the built-in routines runfunction in Qt, corresponding to the installation directory is D: \ Qt \ Qt5.8.0 \ Examples \ Qt-5.8 \ qtconcurrent \ runfucntion

1.1. pro project file

To use the QtConcurrent module, you must add: QT + = concurrent in. pro.

QT += concurrent widgetsCONFIG += consoleCONFIG -= app_bundleSOURCES += main.cpp

1.2 main. cpp

 1 #include <QApplication> 2 #include <QDebug> 3 #include <QThread> 4  5 #include <QString> 6 #include <QtConcurrent> 7  8 void func(QString name) 9 {10     qDebug() << "Hello" << name << "from" << QThread::currentThread();11 }12 13 int main(int argc, char **argv)14 {15     QApplication app(argc, argv);16 17     QFuture<void> fut1 = QtConcurrent::run(func, QString("Thread 1"));18     QFuture<void> fut2 = QtConcurrent::run(func, QString("Thread 2"));19 20     fut1.waitForFinished();21     fut2.waitForFinished();22 }

As you can see, the QtConcurrent: run () function runs func () in two different threads. The output result is as follows:

Hello "Thread 2" from QThread(0x3597f0, name = "Thread (pooled)")Hello "Thread 1" from QThread(0x337720, name = "Thread (pooled)")

The following is a detailed explanation of the use of QtConcurrent: run (). After reading 2 and 3, let's take a look at the above runfunction routine, which is very easy to understand.

 2. Common functions

2.1 run the function in a thread

extern void func();QFuture<void> future = QtConcurrent::run(func);

If you want to specify a thread pool for it, you can pass the pointer of the thread pool as the first parameter.

extern void func();QThreadPool pool;QFuture<void> future = QtConcurrent::run(&pool, func);

2.2 pass parameters to the Function

Parameters to be passed must be added after the function name.

extern void FuncWithArguments(int arg1, const QString &string); int integer = ...; QString string = ...; QFuture<void> future = QtConcurrent::run(FuncWithArguments,integer,string);

2.3 obtain the calculation result of this function

extern QString Func(const QByteArray &input);QByteArray byte_array = ...;QFuture<QString> future = QtConcurrent::run(func, byte_array);...QString result = future.result();

3. member functions

To run a member function in a class in a thread, you can pass the reference or pointer pointing to the class instance as the first parameter of QtConcurrent: run,

Constant member functions generally pass the const reference, while very many member functions generally pass the pointer (pointer)

3.1 constant member functions

In a separate thread, the constant member function split () that calls QByteArray is passed to the run () function. The parameter is bytearray.

  QByteArray bytearray = "hello world";  QFuture<QList<QByteArray> > future = QtConcurrent::run(bytearray, &QByteArray::split, ',');  ...  QList<QByteArray> result = future.result();

3.2 extraordinary member functions

In a separate thread, the invertPixels () function that calls the QImage extraordinary member function is passed to the run () function. The parameter is & image

  QImage image = ...;  QFuture<void> future = QtConcurrent::run(&image, &QImage::invertPixels, QImage::InvertRgba);  ...  future.waitForFinished();
// At this point, the pixels in 'image' have been inverted

 

References:

Qt assistant | Qt 5.8 | Qt Concurrent | Concurrent Run

 

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.