void CSocketThread::run(){ client = QSharedPointer< QTcpSocket >( new QTcpSocket() ); client->setSocketDescriptor( socketDescriptor ); QObject::connect( client.data(), SIGNAL( readyRead() ), this, SLOT( on_dataReceived() ), Qt::QueuedConnection ); exec();}
void CSocketThread::on_dataReceived(){ QTcpSocket* socket = qobject_cast< QTcpSocket* >( sender() ); if( !socket ) { return; } int bytesToRead = socket->bytesAvailable(); QByteArray tmp = socket->read( bytesToRead );..................}
void CTcpServer::incomingConnection( qintptr handle ){ client = QSharedPointer< CSocketThread >( new CSocketThread( handle ) ); QObject::connect( client.data(), &CSocketThread::dataReceived, this, &CTcpServer::on_dataReceived ); QObject::connect( client.data(), &QThread::finished, client.data(), &QObject::deleteLater ); client->start();}
It is important to remember that a QThread instance lives in the old thread that instantiated it, not in the new thread that calls run(). This means that all of QThread's queued slots will execute in the old thread. Thus, a developer who wishes to invoke slots in the new thread must use the worker-object approach; new slots should not be implemented directly into a subclassed QThread.
C++ (Qt)...int bytesToRead = socket->bytesAvailable();QByteArray tmp = socket->read( bytesToRead );... // здесь надо что-то делать с tmp!