class Socket : public QTcpSocket{ Q_OBJECTpublic: ClientServer *window; Socket(int sockDeskr,QObject *parent);public slots: void read(); void read_error(); void deleteLater(); void write();};class Thread : public QThread{ Q_OBJECTpublic: ClientServer *window; Thread(int sockDeskr,QObject *parent); void run(); private: int sockDeskr;};Socket::Socket(int sockDeskr,QObject *parent):QTcpSocket(parent){ window=(ClientServer *)parent; this->setSocketDeskriptor(sockDeskr); connect(this,SIGNAL(readyRead()),this,SLOT(read())); connect(this,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(read_error())); connect(window->pushButton_3,SIGNAL(clicked()),this,SLOT(write()));}void Socket::read(){ QByteArray bar=this->readAll(); QString str; str.append(bar); window->textEdit->setText(str); }void Socket::read_error(){ QMessageBox *mb=new QMessageBox(this); mb->setText(codec->toUnicode("Ошибка сокета: ")+this->errorString()); mb->exec();}void Socket::write(){ QByteArray bar; bar.append(window->textEdit->toPlainText()); this->write(bar);}/////////////////////////////////////////////////////////<-----------Thread-------->/////////////////////////void Thread::Thread(int sockDeskr,QObject *parent){ this->sockDeskr=sockDeskr; window=(ClientServer *)parent;}void Thread::run(){Socket *soc=new Socket(sockDeskr,window);}
tB->cB->moveToThread(tB); tA->cA->moveToThread(tA);
void Thread::Thread(int sockDeskr,QObject *parent){ this->sockDeskr=sockDeskr; window=(ClientServer *)parent;}void Thread::run(){Socket *soc=new Socket(sockDeskr,window);}//...QTcpSocket* socket = new QTcpSocket(this);socket->connectToHost(...);Thread* thread = new Thread(socket->socketDescriptor(), this);
C++ (Qt)Socket *soc=new Socket(sockDeskr,window);