QObject: Cannot create children for a parent that is in a different thread.(Parent is QNativeSocketEngine(0x9dae6e8), parent's thread is MyServerThread(0x9c47180), current thread is QThread(0x9bfc838)
C++ (Qt)==========================================MyServer::MyServer(QWidget *pwgt){ m_ptxt = new QTextEdit; pb_start = new QPushButton("START"); ServerThread = new MyServerThread; //QThread m_ptxt->setReadOnly(true); QVBoxLayout* pvbxLayout = new QVBoxLayout; pvbxLayout->addWidget(new QLabel("<H1>Server</H1>")); pvbxLayout->addWidget(m_ptxt); pvbxLayout->addWidget(pb_start); setLayout(pvbxLayout); connect(pb_start,SIGNAL(clicked()),ServerThread,SLOT(start())); connect(ServerThread,SIGNAL(signal_errorServer(QString)),m_ptxt,SLOT(append(QString))); connect(ServerThread,SIGNAL(signal_New_Connection(QString)),m_ptxt,SLOT(append(QString)));} MyServerThread::MyServerThread(){ m_nNextBlockSize = 0; nPort = 502; m_ptcpServer = new QTcpServer; m_ptcpServer->moveToThread(this);} void MyServerThread::run()//(int nPort, QWidget* pwgt /*=0*/): QWidget(pwgt),m_nNextBlockSize(0){ /* OBRABOTKA OSHIBKI SOEDINENIA */ if(!m_ptcpServer->listen(QHostAddress::Any, nPort)) { emit signal_errorServer(m_ptcpServer->errorString()); m_ptcpServer->close(); return; } connect(m_ptcpServer, SIGNAL(newConnection()), this, SLOT(slotNewConnection()));*/ exec();} /*virtual*/void MyServerThread::slotNewConnection(){ QTcpSocket* pClientSocket = m_ptcpServer->nextPendingConnection(); connect(pClientSocket, SIGNAL(disconnected()), pClientSocket, SLOT(deleteLater()) ); connect(pClientSocket, SIGNAL(readyRead()), this, SLOT(slotReadClient()) ); emit signal_New_Connection("New connection");} void MyServerThread::slotReadClient(){ QTcpSocket* pClientSocket = (QTcpSocket*)sender(); Buffer_IN.clear(); while( pClientSocket->bytesAvailable() ) Buffer_IN += pClientSocket->readAll(); CheckUp_Answer(); sendToClient(pClientSocket, Buffer_OUT);} void MyServerThread::sendToClient(QTcpSocket * pSocket, const QByteArray &arOut){ pSocket->write(arOut);} void MyServerThread::CheckUp_Answer(){ for(int i = 1;i <= 200 ;i++) { Buffer_OUT[i+8] = 2*i; }}
C++ (Qt)======================================class MyServerThread: public QThread{ Q_OBJECT private: int nPort; QByteArray Buffer_IN, Buffer_OUT; QTcpServer * m_ptcpServer; private: void sendToClient(QTcpSocket* pSocket, const QByteArray& arOut); void CheckUp_Answer(); public: MyServerThread(); void run();signals: void signal_New_Connection(QString); void signal_errorServer(QString); public slots: virtual void slotNewConnection(); void slotReadClient();};//**********************************************************************class MyServer : public QWidget{ Q_OBJECT private: MyServerThread *ServerThread; QTextEdit * m_ptxt; QPushButton *pb_start; public: MyServer(QWidget* pwgt = 0);};
C++ (Qt)m_ptcpServer->moveToThread(this);
QObject: Cannot create children for a parent that is in a different thread.
C++ (Qt)MyServerThread::MyServerThread(){ m_nNextBlockSize = 0; nPort = 502;} void MyServerThread::run()//(int nPort, QWidget* pwgt /*=0*/): QWidget(pwgt),m_nNextBlockSize(0){ m_ptcpServer = new QTcpServer(); /* OBRABOTKA OSHIBKI SOEDINENIA */ if(!m_ptcpServer->listen(QHostAddress::Any, nPort)) { emit signal_errorServer(m_ptcpServer->errorString()); m_ptcpServer->close(); return; } connect(m_ptcpServer, SIGNAL(newConnection()), this, SLOT(slotNewConnection()));*/ exec();}
C++ (Qt) //*.h class MyServer : public QTcpServer {...... private slots: void slotReadClient();...} void MyServer::slotReadClient(){ QTcpSocket* pClientSocket = (QTcpSocket*)sender(); Buffer_IN.clear(); while( pClientSocket->bytesAvailable() ) Buffer_IN += pClientSocket->readAll(); CheckUp_Answer(); sendToClient(pClientSocket, Buffer_OUT);}
C++ (Qt)void MyServerThread::run()//(int nPort, QWidget* pwgt /*=0*/): QWidget(pwgt),m_nNextBlockSize(0){ connect(m_ptcpServer, SIGNAL(newConnection()), this, SLOT(slotNewConnection())); forever { m_ptcpServer = new QTcpServer(); { QMutexLocker locker(&mutex); if (!pendingSocket) waitCondition.wait(&mutex) socket = m_ptcpServer.nextPendingConnection(); pendingSocket = false; } /* OBRABOTKA OSHIBKI SOEDINENIA */ if(!m_ptcpServer->listen(QHostAddress::Any, nPort)) { emit signal_errorServer(m_ptcpServer->errorString()); m_ptcpServer->close(); return; } } }
C++ (Qt)void MyServerThread::slotNewConnection(){ mutex.lock(); pendingSocket = true; mutex.unlock(); waitCondition.wakeOne();}