PDA

View Full Version : QUdpSocket change port



sanji
1st March 2020, 22:22
Hi everyone,
I'm developing an application reading from an UDP port. All works fine, before I don't try to change the port it's listening on. Then I got the run-time message:

QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QUdpSocket(0x3564c50), parent's thread is UDPListener(0x35533f0), current thread is QThread(0x39b540)
QObject::killTimer: Timers cannot be stopped from another thread

Socket is created inside a run() function of class which inherits class Listener which is based on QThread()



void UDPListener::run()
{
socket = new QUdpSocket;
socket->bind(port);
connect(socket, SIGNAL(readyRead()), this, SLOT(readUDP()), Qt::DirectConnection);
connect(this, SIGNAL(reconnectNow()), this, SLOT(reconnectPrivate()), Qt::QueuedConnection);
exec();
emit connectionStatus(false);
socket->deleteLater();
}

Slot reconnecting socket and binding to other port:
void UDPListener::reconnectPrivate()
{
socket->disconnectFromHost();
socket->bind(port);
}


Class listener has a method called reconnect() which emits the signal reconnectNow(), connected (inside run()) to reconnectPrivate()


void Listener::reconnect()
{
emit reconnectNow();
}


Because I use Qt::QueuedConnection it should be in this same thread with socket, where the exec() function is. In fact, the binding to the new port works and the socket reconnects, but there's a error message like above, followed by a repetitive error:
QObject::killTimer: Timers cannot be stopped from another thread

Any idea please? I have none anymore.