PDA

View Full Version : QThread and QTcpSocket problem



probine
5th April 2006, 10:26
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:


void Client :: run()
{
cout << "void Client :: run()\n";
tcpSocket = new QTcpSocket();
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:


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() ?

probine
5th April 2006, 10:57
Problem solved:

I was starting the thread in another class. So, I change it to be run from the constructor in the thread.



Client :: Client(int socketDescriptor, Server *server, QObject *parent) : QThread(parent)
{
this->socketDescriptor = socketDescriptor;
this->server = server;
this->message = new Message(this);
chattingUserName = new QString();
messageIn = new QByteArray();
clientLogged = false;
connect(this, SIGNAL(finished()), this, SLOT(finished()));
run();
}