Hi everyone!
A simple question, but cannot find any solution.
I have a class:
{
Q_OBJECT
public:
Test();
int port; //An UDP port to bind to
void run() override; // creates, assigns an QUdpSocket object to the so variable, and binds it to some initial port.
public slots:
void reconnect(); // Should bind socket to some other port, than the one bound while initializing.
void receive(); // Called to receive datagram. Works fine.
};
class Test: public QThread
{
Q_OBJECT
public:
Test();
int port; //An UDP port to bind to
QUdpSocket *so;
void run() override; // creates, assigns an QUdpSocket object to the so variable, and binds it to some initial port.
public slots:
void reconnect(); // Should bind socket to some other port, than the one bound while initializing.
void receive(); // Called to receive datagram. Works fine.
};
To copy to clipboard, switch view to plain text mode
The run() function looks like (simplyfied):
void Test::run()
{
so->bind(port);
this->exec();
so->deleteLater();
}
void Test::run()
{
so = new QUdpSocket();
so->bind(port);
this->exec();
so->deleteLater();
}
To copy to clipboard, switch view to plain text mode
As a result, the QUdpSocket *so lives in its own thread.
Is there some way to run reconnect() slot inside this same thread, where the run() function and UdpSocket *so live in? Otherwise the bind to another port isn't allowed:
QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
QObject: Cannot create children for a parent that is in a different thread.
I've tried a lot. Always ending with running the SLOT reconnect() inside the SIGNALs thread.
Please, give me any suggestion, because I stuck out of ideas - since days now.
Bookmarks