zoukankan      html  css  js  c++  java
  • Qt::线程::继承QObject

    class Worker : public QObject
    {
        Q_OBJECT
    public:
        explicit Worker(QObject *parent = 0);
    
        void WorkRun();
        void setFlag(bool flag = true);
    signals:
        void signal_back();//处理结果返回信号
    
    private:
        bool isStop;
    };
    #include "controller.h"
    #include <QDebug>
    
    Worker::Worker(QObject *parent) : QObject(parent)
    {
        isStop = false;
    }
    
    void Worker::WorkRun()        //线程处理函数:具体处理的事情
    {
        while(!isStop)
        {
            QThread::sleep(1);
            emit signal_back();    //发送返回信号
            qDebug() << "the child thread number:" << QThread::currentThread();
        }
    }
    
    void Worker::setFlag(bool flag)
    {
        isStop = flag;
    }
    subthread = new QThread(this);
    m_MyThread = new Worker();
    m_MyThread->moveToThread(subthread);
    
    connect(this, &Widget::StartThread, m_MyThread, &Worker::WorkRun);
    connect(m_MyThread, &Worker::signal_back, this, &Widget::slot_handle_finish);
    subthread->start();
    emit StartThread();
  • 相关阅读:
    Codeforces 385C
    Codeforces 496C
    HDU 6114 Chess
    Codeforces 839B
    Codeforces 483B
    Codeforces 352B
    Codeforces 768B
    Codeforces 38B
    Codeforces 735B
    Codeforces 534B
  • 原文地址:https://www.cnblogs.com/osbreak/p/15632538.html
Copyright © 2011-2022 走看看