QT multithreading notes and qt Multithreading

Source: Internet
Author: User

QT multithreading notes and qt Multithreading

1. it is not convenient to use QThread when QT multithreading involves a large amount of data interaction between the main thread and sub-thread, because the run () function itself cannot accept any parameters, therefore, data can only be obtained through the interaction between the signal and the slot. If only the simple interaction of data is successful, if the interaction between the main and sub-threads is involved, this method is inconvenient. Here we use another method to create a class that inherits from QObject and perform the movetoThread () operation:

class FileThread :public QObject{    Q_OBJECTpublic:    QString m_path;    QFile *m_file;signals:    void readend(QStringList headerList, QVector < QStringList > listArray, long row, long column);public slots:    void getFile(QString path);};

In the GUI thread, perform the following settings:

FileThread * myfilethread = new FileThread (); QThread * thread = new QThread; // The following two rows are used for registration type. Data transmitted between QT threads must be of the metadata type, qregisterype <QStringList> ("QStringList"); qregister1_ype <QVector <QStringList> ("QVector <QStringList>"); myfilethread-> moveToThread (thread ); connect (this, SIGNAL (startread (QString), myfilethread, SLOT (getFile (QString); connect (myfilethread, SIGNAL (readend (QStringList, QVector <QStringList>, long, long), this, SLOT (showdata (QStringList, QVector <QStringList>, long, long); thread-> start (); emit startread (path );

2. note: Data Interaction between threads only supports the metadata type. Some complex types of combinations must be registered before interaction, the registration method is qregisterpolicype <QVector <QStringList> ("QVector <QStringList> ");

3. multithreading is usually used: when a large number of loop operations are involved in the main GUI thread, the interface will die. No matter the dialog box or anything, the expected data will be displayed, in this case, you need to set up another thread for the loop operation. In addition, note that you cannot create a new UI operation in the Child thread.

 


Qt Multithreading

This does not seem to be as complicated as multithreading... if it is just this function, a main thread is enough.

How to set the number of threads in qt multi-thread programming?

It was not long before I learned the thread module of QT. The implementation scheme is to inherit the QThread class and write a new Thread class.
Create a new Thread and call the run function to start the Thread when the main process is to be called. These threads can be stored in arrays to facilitate the management of these threads by the main thread.

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.