I'm writing a network library that wraps the QUdpSocket:
QAbstractSocket *UdpNetworkStreamer::addConnection()
{
QUdpSocket *udpSocket = new QUdpSocket(this);
udpSocket->bind(connection.port, QUdpSocket::ShareAddress);
bool ret = udpSocket->joinMulticastGroup(QHostAddress(connection.ip));
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::QueuedConnection);
return udpSocket;
}
- create a new
QUdpSocket. - connect to its
readyReadsignal. - call
readDatagramwhenreadyReadis raised.
All is working fine when I use the library from a Qt GUI application.
The problem starts when another user includes the library used outside of a Qt GUI application.
He calls the addConnection (which creates the socket and calls connect on the readyRead)
The thread on which the addConnection is called is non-Qt.
The addConnection seems to end successfully but the readyRead is never emitted.
Calling read (even though no readyRead was emitted) leads to a successful datagram read.
Fixes that did not work :
moving the the UDP socket thread to the this->thread
QUdpSocket *udpSocket = new QUdpSocket(); udpSocket->moveToThread(this->thread()); udpSocket->setParent(this);I tried to simulate the problem by calling:void
MainWindow::on__btnOpenMulticastReceiver_clicked() { QFuture<void> future = QtConcurrent::run(this, &MainWindow::CreateMulticastConnection, testHandle); }This also led to same symptoms as the one the user had with my library, meaning the
readyReadwasn't emitted.QSignalSpy- I've activated a spy on thereadyReadsignal; the counter kept on being zero although I could read data directly from the socket. The spy gave valid results (i.e. progressed) when used the socket was initialized on the main thread.
My Questions:
- What am I missing and doing wrong ?
- What is the simplest way of having the
readyReademitted even though it is not created on the main GUI thread - I couldn't find any sample that works with no GUI or outside Qt threads.