美文网首页
QT线程--moveToThread()

QT线程--moveToThread()

作者: Magic11 | 来源:发表于2019-06-26 11:43 被阅读0次
    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

    相关文章

      网友评论

          本文标题:QT线程--moveToThread()

          本文链接:https://www.haomeiwen.com/subject/ikaqcctx.html