QThread使用心得

假設只有main線程和子線程child函數

class child :public QThread
{
    Q_OBJECT
public:
    child ( QThread* parent = nullptr ){
    moveToThread ( this );
}

virtual void run (){
    exec ();
}

public slots :
    void Recv( LPVOID param ){
         int i = 0;
    }
};

1. Recv默認是在main中執行,要想使Recv在child中執行,只需在child 構造函數中加一句this

moveToThread ( this );

2. 還有種方法是子類化一個QObject對象,而後把對象move到child中:線程

class obj:public QObject
{
    Q_OBJECT

public slots :
    void RecvParam ( LPVOID param ){
        int i = 0;
    }

};    

child* thd = new child;
obj Obj;
Obj.moveToThread ( thd );
thd->start ();

歡迎討論code

相關文章
相關標籤/搜索