qtcpsocket with qthread QObject: Cannot create children for a parent that is in a different thread
-
Hi ,
anyone can help me about qtcpsocket with qthread
Application logs:
Connected client 16 QObject: Cannot create children for a parent that is in a different thread. (Parent is QTcpSocket(0x7fce4ed06720), parent's thread is QThread(0x7fce4ed02520), current thread is UpdataServerThread(0x7fce4ed82f00) connected_client_socket => QHash((QHostAddress("::ffff:127.0.0.1"), QTcpSocket(0x7fce4ed06720))) connected_client_ip => QHash((QHostAddress("::ffff:127.0.0.1"), QTcpSocket(0x7fce4ed06720))) QHostAddress("::ffff:127.0.0.1") in : asd connected_client_socket => QHash((QHostAddress("::ffff:127.0.0.1"), QTcpSocket(0x7fce4ed06720))) connected_client_ip => QHash((QHostAddress("::ffff:127.0.0.1"), QTcpSocket(0x7fce4ed06720))) QHostAddress("::ffff:127.0.0.1") in : asd connected_client_socket => QHash((QHostAddress("::ffff:127.0.0.1"), QTcpSocket(0x7fce4ed06720))) connected_client_ip => QHash((QHostAddress("::ffff:127.0.0.1"), QTcpSocket(0x7fce4ed06720))) QHostAddress("::ffff:127.0.0.1") in : asd connected_client_socket => QHash((QHostAddress("::ffff:127.0.0.1"), QTcpSocket(0x7fce4ed06720))) connected_client_ip => QHash((QHostAddress("::ffff:127.0.0.1"), QTcpSocket(0x7fce4ed06720))) QHostAddress("::ffff:127.0.0.1") in : asd connected_client_socket => QHash((QHostAddress("::ffff:127.0.0.1"), QTcpSocket(0x7fce4ed06720))) connected_client_ip => QHash((QHostAddress("::ffff:127.0.0.1"), QTcpSocket(0x7fce4ed06720))) QHostAddress("::ffff:127.0.0.1") in : sad connected_client_socket => QHash((QHostAddress("::ffff:127.0.0.1"), QTcpSocket(0x7fce4ed06720))) connected_client_ip => QHash((QHostAddress("::ffff:127.0.0.1"), QTcpSocket(0x7fce4ed06720))) QHostAddress("::ffff:127.0.0.1") in : sad Connected client 19 QObject: Cannot create children for a parent that is in a different thread. (Parent is QTcpSocket(0x7fce4eea12f0), parent's thread is QThread(0x7fce4ed02520), current thread is UpdataServerThread(0x7fce4ee137e0) connected_client_socket => QHash((QHostAddress("::ffff:192.168.200.36"), QTcpSocket(0x7fce4eea12f0))) connected_client_ip => QHash((QHostAddress("::ffff:192.168.200.36"), QTcpSocket(0x7fce4eea12f0))) QHostAddress("::ffff:192.168.200.36") in : asdasdas connected_client_socket => QHash((QHostAddress("::ffff:127.0.0.1"), QTcpSocket(0x7fce4ed06720))) connected_client_ip => QHash((QHostAddress("::ffff:127.0.0.1"), QTcpSocket(0x7fce4ed06720))) QHostAddress("::ffff:127.0.0.1") in : asd
what is wrong about this ?
my code -
@rifky said in qtcpsocket with qthread QObject: Cannot create children for a parent that is in a different thread:
Connected client 19
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QTcpSocket(0x7fce4eea12f0), parent's thread is QThread(0x7fce4ed02520), current thread is UpdataServerThread(0x7fce4ee137e0)What information are you looking for which is not clear from this error message?
-
You should take a look at QTcpServer::incomingConnection(qintptr)
-
Hi and welcome to devnet,
You are either moving a socket to a different thread or using it directly in that different thread.
In both case it's the wrong thing to do.
Follow @Christian-Ehrlicher advice and move the socket creation in that other thread.
-
@SGaist said in qtcpsocket with qthread QObject: Cannot create children for a parent that is in a different thread:
Hi and welcome to devnet,
You are either moving a socket to a different thread or using it directly in that different thread.
In both case it's the wrong thing to do.
Follow @Christian-Ehrlicher advice and move the socket creation in that other thread.
thx for response,
i followed this ,i little bit confused how to implement QThread with qtcpsocket,can you give me different concept? aside from
server start server is listening client connected thread start
before this i made without thread but when two device is connected and server can receive data from them, but I found anomalies , server can't handle both of them, looks like race condition on readyRead() function.
i guessing with qthread can solve that problem.
-
@rifky said in qtcpsocket with qthread QObject: Cannot create children for a parent that is in a different thread:
but I found anomalies , server can't handle both of them, looks like race condition on readyRead() function.
You should rather sort this out.
i guessing with qthread can solve that problem.
I'm guessing that is more likely to create problems than solve them!
Though I may be wrong. If you do want to use threads have you looked at the example https://doc.qt.io/qt-5/qtnetwork-blockingfortuneclient-example.html ? But if it were me I would look at the non-blocking https://doc.qt.io/qt-5/qtnetwork-fortuneserver-example.html. There are no threads there, and it seems much simpler to me. Unless @Christian-Ehrlicher or @SGaist say otherwise....
-
@JonB said in qtcpsocket with qthread QObject: Cannot create children for a parent that is in a different thread:
Though I may be wrong. If you do want to use threads have you looked at the example https://doc.qt.io/qt-5/qtnetwork-blockingfortuneclient-example.html ? But if it were me I would look at the non-blocking https://doc.qt.io/qt-5/qtnetwork-fortuneserver-example.html. There are no threads there, and it seems much simpler to me. Unless @Christian-Ehrlicher or @SGaist say otherwise....
I've tried that, i made it into support with multi clients, i put peerAddress with socket pointer in qHash,
before this i made without thread but when two device is connected and server can receive data from them, but I found anomalies , server can't handle both of them, looks like race condition on readyRead() function.
but in this case different with this topic .
anyone can help me to fix my code ?
-
@rifky said in qtcpsocket with qthread QObject: Cannot create children for a parent that is in a different thread:
anyone can help me to fix my code?
which code?
-
@Christian-Ehrlicher There is a link to a zip file download at the end of his first post :)
-
You have to create the socket in the run() function as only there the new thread is running. See https://doc.qt.io/qt-5/qthread.html#details
-
@Christian-Ehrlicher said in qtcpsocket with qthread QObject: Cannot create children for a parent that is in a different thread:
You have to create the socket in the run() function as only there the new thread is running. See https://doc.qt.io/qt-5/qthread.html#details
its solved , i have tried to create socket in run() function
void UpdataServerThread::run() { updata_socket = new QTcpSocket(); if (!updata_socket->setSocketDescriptor(socketDescriptor)) { emit error(updata_socket->error()); return; } connect(updata_socket,SIGNAL(readyRead()),this,SLOT(readyRead()),Qt::QueuedConnection); connect(updata_socket,SIGNAL(disconnected()),this,SLOT(disconnected()),Qt::DirectConnection); connected_client_socket.insert(updata_socket->peerAddress(),updata_socket); qDebug()<<"creating socket ptr"; updata_socket->write("Welecome Message"); exec(); }
Thanks alot sir ,, i got new another problem with sending/write data on socket maybe i shoud make new topic for this