QThread and QTcpSocket problem
I have a thread and a tcpSocket. The tcpSocket is declared in my "x.h" class.
At some point I want to use this tcpSocket, but I get this message:
ASSERT failure in QObject::QObject(): "Cannot create children for a parent that is in a different thread.", file kernel/qobject.cpp, line 599
This is the code where I create my tcpSocket:
Code:
void Client :: run()
{
cout << "void Client :: run()\n";
if(!tcpSocket->setSocketDescriptor(socketDescriptor))
{
cout << "Error - tcpSocket descriptor\n";
return;
}
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readData()));
connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
exec();
}
This is the code where I want my tcpSocket to write to another computer:
Code:
void Client
:: loginResponse(bool accepted,
QString *userName,
QByteArray *messageOut
) {
if(accepted==true)
{
server->addClient(*userName, this);
clientLogged = true;
tcpSocket->write("hello\n");
}
}
How can i refer to the tcpSocket that was instantiated in the run() ?
Re: QThread and QTcpSocket problem
Problem solved:
I was starting the thread in another class. So, I change it to be run from the constructor in the thread.
Code:
Client
:: Client(int socketDescriptor, Server
*server,
QObject *parent
) : QThread(parent
){
this->socketDescriptor = socketDescriptor;
this->server = server;
this->message = new Message(this);
clientLogged = false;
connect(this, SIGNAL(finished()), this, SLOT(finished()));
run();
}