C++ (Qt)fSocket = new QTcpSocket(this);
C++ (Qt)fSocket = new QTcpSocket();fSocket->moveToThread(this);
C++ (Qt)Thread::run(){...fSocket = new QTcpSocket();...}
C++ (Qt)Thread::run(){...fSocket = new QTcpSocket(this);...}
C++ (Qt)class HeavyJob : public QObject{ Q_OBJECTpublic: explicit HeavyJob(QObject *parent = 0); bool isRunning(); public slots: void start(); void stop(); private: bool running;};
C++ (Qt)HeavyJob::HeavyJob(QObject *parent) : QObject(parent), running(false){} bool HeavyJob::isRunning(){ return running;} void HeavyJob::start(){ if (running) return; running = true; qDebug() << "started"; while(running) { for(int i = 0; i < 1000; ++i); } qDebug() << "stopped";} void HeavyJob::stop(){ qDebug() << "stop()"; running = false;}
C++ (Qt)HeavyJob *job = new HeavyJob(); QThread *worker = new QThread(this); job->moveToThread(worker); worker->start();
C++ (Qt)if (!job->isRunning()) {// job->start(); // 1 QTimer::singleShot(10, job, SLOT(start())); // 2 } else { job->stop(); // 3// QTimer::singleShot(10, job, SLOT(stop())); // 4 }
C++ (Qt)this->moveToThread(&thread);thread.start();
int m_timeInterval = 100;QTime m_elapsedTime;m_elapsedTime.start();if(m_elapsedTime.elapsed() > m_timeInterval){m_elapsedTime.start();m_elapsedTime.addMSecs(m_timeInterval);//do smthn}