class MyMoveToThreadFunc : public QObject
{
Q_OBJECT
public:
void showObjectThreadID()
{
qDebug()<<"# MyMoveToThreadFunc thread id = "<<QThread::currentThreadId();
}
signals:
void again();
public slots:
void slotOfThread()
{
qDebug()<<" MyMoveToThreadFunc slot thread id = "<<QThread::currentThreadId();
emit again();
}
};
class MyTry : public QObject
{
Q_OBJECT
public:
MyTry();
QThread *mThread;
MyMoveToThreadFunc *mFunc;
};
MyTry::MyTry()
{
mThread = new QThread();
qDebug()<<"main thread id = "<<QThread::currentThreadId();
mFunc = new MyMoveToThreadFunc();
mFunc->moveToThread(mThread);
QObject::connect(mThread, SIGNAL(started()), mFunc, SLOT(slotOfThread()));//slot将会在mThread中运行
mFunc->showObjectThreadID()//在主程序运行。
mThread->start();//启动线程
}
参考: https://www.jianshu.com/p/9eee3c5208c4
网友评论