Thread and Socket
-
This is a work in progress, so I please don't expect it to be completed finished code:
void SckThread::run() { QTcpSocket* pSocket = new QTcpSocket(); if ( pSocket == nullptr ) { return; } //Set the ID if( !pSocket->setSocketDescriptor(msckDescriptor) ) { //Something's wrong, we just emit a signal emit error(pSocket->error()); return; } //Connect socket and signal pSocket->setSocketOption(QAbstractSocket::LowDelayOption, 1); QEventLoop evtLoop; //pSocket->moveToThread(this->thread()); mpsckIncoming = pSocket; auto conn1 = QObject::connect(mpsckIncoming ,QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error) ,this, &SckThread::onErrorOccurred); auto conn2 = QObject::connect(mpsckIncoming, &QAbstractSocket::disconnected ,&evtLoop, [&evtLoop]() { qinf() << "Client disconnected"; evtLoop.quit(); }); auto conn3 = QObject::connect(mpsckIncoming, &QAbstractSocket::readyRead ,this, &SckThread::onReadyRead); //We'll have multiple clients, we want to know which is which qinf() << "Client connected"; //Make this thread a loop, //thread will stay alive so that signal/slot to function properly //not dropped out in the middle when thread dies evtLoop.exec(); QObject::disconnect(conn1); QObject::disconnect(conn2); QObject::disconnect(conn3); //Cleanup delete pSocket; }
I'm getting a message up in the Application Output:
QObject: Cannot create children for a parent that is in a different thread.
I'm not sure why and as you can see in the function I've tried commenting out the line:
pSocket->moveToThread(this->thread());
Still occurs, with this line uncommented.
-
@SPlatten said in Thread and Socket:
Still occurs, with this line uncommented.
I told you many and many times not to sub-class QThread but instead use "worker class".
There is a rule with Qt: Do not sub-class QThread when using with QObject based instances==> I would be great, at least for you, if you could accept this! (cf. https://www.vikingsoftware.com/how-to-use-qthread-properly/)
Do it on the right way, and you will resolve your issue.
-
@SPlatten said in Thread and Socket:
I've solved this problem now. Just moving the call to moveToThread to after setSocketOption.
I am not sure you really solve the problem, but it is your choice.
I can not understand why you always do same errors, ask for help and then refuse to accept advice that are given from others. -
And you doing it wrong no matter how often we tell you how to do it right...
-
@SPlatten said in Thread and Socket:
There are numerous examples of sub-classing QThread
https://doc.qt.io/qt-5/qthread.html#details : "new slots should not be implemented directly into a subclassed QThread."
-
@SPlatten said in Thread and Socket:
Can you accept that you might not know it all?
Yes, I can. I am not a C++ expert, nor a Qt expert.
There are numerous examples of sub-classing QThread and sub-classing is standard OOP.
Of course, sub-classing is OOP basic, but that's not the point here.
What I try to tell you is that sub-classing QThread is in general the wrong way to do.
Especially when using with QObject.
That is not my point of view, but Qt recommendation, which is the company which have create all those code.I have tried to explain it in many ways, I give you links to Qt documentation, to blogs which explains you why it is wrong.
But you don't want to learn.Again, your choice.