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.