Results 1 to 2 of 2

Thread: QThread and QTcpSocket problem

  1. #1
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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:
    Qt Code:
    1. void Client :: run()
    2. {
    3. cout << "void Client :: run()\n";
    4. tcpSocket = new QTcpSocket();
    5. if(!tcpSocket->setSocketDescriptor(socketDescriptor))
    6. {
    7. cout << "Error - tcpSocket descriptor\n";
    8. return;
    9. }
    10. connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readData()));
    11. connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
    12. exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    This is the code where I want my tcpSocket to write to another computer:
    Qt Code:
    1. void Client :: loginResponse(bool accepted, QString *userName, QByteArray *messageOut)
    2. {
    3. if(accepted==true)
    4. {
    5. server->addClient(*userName, this);
    6. clientLogged = true;
    7. tcpSocket->write("hello\n");
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 


    How can i refer to the tcpSocket that was instantiated in the run() ?

  2. #2
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

    Qt Code:
    1. Client :: Client(int socketDescriptor, Server *server, QObject *parent) : QThread(parent)
    2. {
    3. this->socketDescriptor = socketDescriptor;
    4. this->server = server;
    5. this->message = new Message(this);
    6. chattingUserName = new QString();
    7. messageIn = new QByteArray();
    8. clientLogged = false;
    9. connect(this, SIGNAL(finished()), this, SLOT(finished()));
    10. run();
    11. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Challenging QTcpSocket reliability problem
    By Raccoon29 in forum Qt Programming
    Replies: 3
    Last Post: 13th January 2009, 10:38
  2. QThread and QTcpSocket
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 19th May 2008, 17:43
  3. QThread and QTcpSocket
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 12th May 2008, 13:06
  4. Problem with QTcpSocket in QThread
    By Raistlin in forum Qt Programming
    Replies: 8
    Last Post: 6th October 2007, 12:23
  5. Problems with QThread and QTcpSocket
    By cookiem in forum Qt Programming
    Replies: 6
    Last Post: 2nd November 2006, 08:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.