I am learning about Multi-threaded programming in Qt, and then while I'm calling the write function of the QTcpSocket
class from a QThread
.The function output is:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QNativeSocketEngine(0x6f1840), parent's thread is QThread(0x624e90), current thread is QThread(0x716ed0)
The code that I called the write function:
QString Processor::GetSystemInfoOfClient() const
{
QString result;
const char *send_buffer = "GSIOC";
char receive_buffer[100];
this->cli_sock->write(send_buffer);
this->cli_sock->waitForBytesWritten();
this->cli_sock->waitForReadyRead(100000);
this->cli_sock->read(receive_buffer, 100);
result = QString(receive_buffer);
return result;
}
The code that I accept the connections:
void Processor::Accept()
{
this->cli_sock = m_server->nextPendingConnection();
this->cli_addr = this->cli_sock->peerAddress();
this->cli_port = this->cli_sock->peerPort();
}
The definition of the connection processor:
class Processor : public QObject
{
Q_OBJECT
public:
explicit Processor(QObject *parent = 0, QTcpServer *server = nullptr);
private:
QTcpServer *m_server = nullptr;
QTcpSocket *cli_sock = nullptr;
QHostAddress cli_addr;
quint16 cli_port;
QString GetSystemInfoOfClient() const;
signals:
void GetDetailedFinished(const QString &address,const QString &port, const QString &SystemInfo);
public slots:
void GetClientDetail();
void Accept();
};
Then I move the processor to the QThread
Processor *processor = new Processor(0, server);
processor->moveToThread(&processor_thread);
connect(&processor_thread, &QThread::finished, processor, &QObject::deleteLater);
connect(this, &Processor_Controller::Accept, processor, &Processor::Accept);
connect(this, &Processor_Controller::GetClientDetail, processor, &Processor::GetClientDetail);
connect(processor, &Processor::GetDetailedFinished, this, &Processor_Controller::PassClientDetail);
processor_thread.start();
And I create the QTcpServer
in the main thread.
// The address of the server
QHostAddress srv_addr("127.0.0.1");
// The server listen port
quint16 srv_port = 9895;
Processor_Controller *controller = new Processor_Controller(0, &server);
connect(&server, &QTcpServer::newConnection, controller, &Processor_Controller::BeginProcess);
connect(controller, &Processor_Controller::DisplayClientDetail, this, &MainWindow::DisplayClientDetail);
// BeginListen
if(!server.listen(srv_addr, srv_port))
emit statusBar()->showMessage("Listen Failed");
else
emit statusBar()->showMessage("Listen Success");
The function that call to the GetSystemInfoOfClient
function:
void Processor::GetClientDetail()
{
QString address = this->cli_addr.toString();
QString port = QString::number(this->cli_port);
QString SystemInfo = this->GetSystemInfoOfClient();
emit this->GetDetailedFinished(address, port, SystemInfo);
}
The constructor of the Processor:
Processor::Processor(QObject *parent, QTcpServer *server) : QObject(parent)
{
this->m_server = server;
}
I just want to create a server in main thread and process the connections in the Processor class. How I can avoid this error without changing this thinking?
I solve this problem just using these two line of codes in Processor::Accept
function
QTcpSocket *m_socket = m_server->nextPendingConnection();
this->cli_sock->setSocketDescriptor(m_socket->socketDescriptor(), m_socket->state(), m_socket->openMode());