banner
Alexeisie

AlexEisie

啊? Email: alexeisie@brs.red
github

QThread Notes

Reference article:
https://blog.csdn.net/u012635648/article/details/89504115

  1. Asynchronous thread class design

Notify the thread object to be destroyed when the thread's lifetime ends.
Thread objects can only be created in the heap space and cannot be actively destroyed by the outside world.
Call the deleteLater() function at the end of the run function.
The thread function actively requests the destruction of the thread object.
Usage:
The lifetime of the thread is uncontrollable and needs to run in the background for a long time.

#ifndef ASYNCTHREAD_H
#define ASYNCTHREAD_H

#include <QThread>

class AsyncThread : public QThread
{
  Q_OBJECT
protected:
  void run()
  {

    deleteLater();
  }
  explicit AsyncThread(QObject* parent = 0):QThread(parent)
  {
 
  }
  ~AsyncThread()
  {

  }
public:
  static AsyncThread* newThread(QObject* parent = 0)
  {
    return new AsyncThread(parent);
  }
};

#endif // ASYNCTHREAD_H
  1. Subclassing QThread

Two ways to use QThread:
(1) Without using an event loop
A. Subclass QThread
B. Rewrite the run function, which contains a while or for loop
C. Set a flag to control the exit of the loop.
Suitable for executing time-consuming operations in the background, such as file copying and network data reading.
(2) Using an event loop.
A. Subclass QThread
B. Rewrite run to call QThread::exec(), starting the thread's event loop
C. Define signals and slots for the subclass. Since the slot function does not run in the newly opened thread, call moveToThread(this) in the constructor.
Suitable for transactional operations, such as file reading and writing, and database reading and writing.

  1. Signal-slot method

Using signals and slots to solve the communication between multiple threads and GUI components:
A. Define update signals for GUI components in the child thread
B. Define slot functions in the main window class to update GUI components
C. Connect the update signal to the slot function asynchronously
The child thread updates the GUI components by sending signals. All GUI component objects can only be attached to the GUI thread (main thread).
The essence of updating the interface status in the child thread is that the child thread sends a signal to notify the main thread of the update request, and the main thread modifies the GUI components based on the specific signal and signal parameters.
Use signals and slots to update the progress display information of the progress bar in the main interface in the child thread.
Work thread class:

#ifndef WORKTHREAD_H
#define WORKTHREAD_H
#include <QThread>

class WorkThread : public QThread
{
  Q_OBJECT
signals:
  void signalProgressValue(int value);
protected:
  void run()
  {
    work();
    exec();
  }
public:
  WorkThread()
  {
    m_stop = false;
    moveToThread(this);
  }
  void work()
  {
    for(int i = 0; i < 11; i++)
    {
        emit signalProgressValue(i*10);
        sleep(1);
    }
  }
};
 
#endif // WORKTHREAD_H

Main interface class:
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QProgressBar>
#include "WorkThread.h"

class Widget : public QWidget
{
  Q_OBJECT
  QProgressBar* m_progress;//progress bar
  WorkThread* m_thread;//work thread
public:
  Widget(QWidget *parent = 0):QWidget(parent)
  {
    m_progress = new QProgressBar(this);
    m_progress->move(10, 10);
    m_progress->setMinimum(0);
    m_progress->setMaximum(100);
    m_progress->setTextVisible(true);
    m_progress->resize(100, 30);
    m_thread = new WorkThread();
    m_thread->start();
    connect(m_thread, SIGNAL(finished()), m_thread, SLOT(deleteLater()));
    //Connect the signal of the work thread to the slot function of the interface
    connect(m_thread, SIGNAL(signalProgressValue(int)), this, SLOT(onProgress(int)));
  }
  ~Widget()
  {
  }
protected slots:
  void onProgress(int value)
  {
    m_progress->setValue(value);
  }
};
#endif // WIDGET_H

Main function:
#include "Widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  Widget w;
  w.show();

  return a.exec();
}
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.