qt 线程简单学习

  1. QThread线程,只需继承QThread类,并重载run方法,之后就可以使用了。
    #ifndef THREAD_H
    #define THREAD_H
    
    #include <QThread>
    
    class Thread : public QThread
    {
        Q_OBJECT
    public:
        Thread()
        {
    
        }
    
    protected:
        void run()
        {
            //add code
        }
    };
    
    #endif // THREAD_H
  2. 也可以继承QObject对象,然后在派生类中声明一个QThread对象,在使用QObject的moveToThread方法,再借助信号,槽机制就可以实现多线程了。
    #ifndef THREAD_H
    #define THREAD_H
    
    #include <QThread>
    #include <QObject>
    
    class Thread : public QObject
    {
        Q_OBJECT
    public:
        Thread()
        {
            this->moveToThread(&thread);
            thread.start();
        }
    
    private:
        QThread thread;
    };
    
    #endif // THREAD_H
原文地址:https://www.cnblogs.com/jecyhw/p/5326738.html