C++ (Qt)ReadData::ReadData(QObject *parent) : QThread(parent){ m_Socket=new QTcpSocket(this); moveToThread(this);}
C++ (Qt)void SBThread::run(){ m_pTcpSocket = new QTcpSocket(this); connect(m_pTcpSocket, SIGNAL(connected()), SLOT(slotConnected())); connect(m_pTcpSocket, SIGNAL(disconnected()), SLOT(disconnected())); connect(m_pTcpSocket, SIGNAL(readyRead()), SLOT(readyRead())); connect(m_pTcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(error(QAbstractSocket::SocketError)));....
C++ (Qt)void SBThread::run(){ QTcpSocket m_pTcpSocket; connect(&m_pTcpSocket, SIGNAL(connected()), SLOT(slotConnected())); connect(&m_pTcpSocket, SIGNAL(disconnected()), SLOT(disconnected())); connect(&m_pTcpSocket, SIGNAL(readyRead()), SLOT(readyRead())); connect(&m_pTcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(error(QAbstractSocket::SocketError)));....
C++ (Qt)class Worker : public QObject{ Q_OBJECT public slots: void doWork(const QString ¶meter) { QString result; /* ... here is the expensive or blocking operation ... */ emit resultReady(result); } signals: void resultReady(const QString &result);}; class Controller : public QObject{ Q_OBJECT QThread workerThread;public: Controller() { Worker *worker = new Worker; worker->moveToThread(&workerThread); connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater); connect(this, &Controller::operate, worker, &Worker::doWork); connect(worker, &Worker::resultReady, this, &Controller::handleResults); workerThread.start(); } ~Controller() { workerThread.quit(); workerThread.wait(); }public slots: void handleResults(const QString &);signals: void operate(const QString &);};
C++ (Qt)class WorkerThread : public QThread{ Q_OBJECT void run() Q_DECL_OVERRIDE { QString result; /* ... here is the expensive or blocking operation ... */ emit resultReady(result); }signals: void resultReady(const QString &s);}; void MyObject::startWorkInAThread(){ WorkerThread *workerThread = new WorkerThread(this); connect(workerThread, &WorkerThread::resultReady, this, &MyObject::handleResults); connect(workerThread, &WorkerThread::finished, workerThread, &QObject::deleteLater); workerThread->start();}
C++ (Qt)if (!m_pTcpSocket) { m_pTcpSocket = new QTcpSocket(this);}
C++ (Qt)thread=new QThread(this);socket=new QTcpSocket(this);socket->moveToThread(thread);
C++ (Qt)thread=new QThread(this);QTcpSocket socket;socket->moveToThread(thread);
C++ (Qt)class Calculator : public QObject{Q_OBJECTsignals: void finished (int sum);public slots: void calculate () { int result = 0; for (int i = 0; i < 99999; ++i) { result += i; } emit finished (result); }} .............................Calculator *calculator = new Calculator;QThread *thread = new QThread calculator->moveToThread (thread);connect(thread, &QThread::started, calculator, &Calculator::calculate);connect(thread, &QThread::finished, calculator, &Calculator::deleteLater); connect(calculator, &Calculator::finished, [] (int result) {qDebug () << result;});connect(calculator, &Calculator::finished, thread, &QThread::quit);connect(calculator, &Calculator::destroyed, thread, &QThread::deleteLater);thread->start();