QT线程--moveToThread()
2019-06-26 本文已影响0人
Magic11
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();//启动线程
}