Hello,
I'm new to Qt and I have this project where I am trying to connect the signal of a thread to another new one, when it is created.
I am working with Qt 5.2.1 .
I get the following Error as soon as "spreadData" is emitted:
Cannot create children for a parent that is in a different thread. Parent is QNativeSocketEngine... Parents thread is MyThread... Current thread is QThread...
MyServer.cpp:
void MyServer::incomingConnection(qintptr socketDescriptor)
{
MyThread *thread = new MyThread(socketDescriptor, this);
connect(thread, SIGNAL(sendData(QByteArray)),this,SLOT(getData(QByteArray)));
connect(this,SIGNAL(spreadData(QByteArray)),thread,SLOT(writeData(QByteArray))); //Error occurring here
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}
void MyServer::getData(QByteArray Data)
{
if(!Data.isNull())
{
emit spreadData(Data);
}
}
MyThread.cpp:
void MyThread::run()
{
socket = new QTcpSocket();
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()),Qt::DirectConnection);
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
exec();
}
QByteArray MyThread::getData()
{
return this->Data;
}
void MyThread::writeData(QByteArray newData)
{
socket->write(newData);
}
I have been searching a long time for a solution but couldn't find any, so please help me. Thanks
Bookmarks