Hello!
I have a small issue, obviously with a QThread. It's about a very simple UDP server that periodically sends out a test string. It does the job, but when I start it, it gives me a an error message and I don't understand what's wrong. Can anyone please explain me? Here is the code:
#include <QtCore>
#include <QCoreApplication>
#include "UDPTestServer.h"
int main(int argc, char *argv[])
{
UDPTestServer udp;
udp.start();
return a.exec();
}
#include <QtCore>
#include <QCoreApplication>
#include "UDPTestServer.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
UDPTestServer udp;
udp.start();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
#include <QThread>
#include <QUdpSocket>
class UDPTestServer
: public QThread{
public:
UDPTestServer();
virtual ~UDPTestServer();
void run();
private:
};
void UDPTestServer::run()
{
while (true)
{
int bytes
= udpSocket.
writeDatagram(datagram.
data(), datagram.
size(),
QHostAddress::LocalHost,
7755);
sleep(1);
}
}
#include <QThread>
#include <QUdpSocket>
class UDPTestServer : public QThread
{
public:
UDPTestServer();
virtual ~UDPTestServer();
void run();
private:
QUdpSocket udpSocket;
};
void UDPTestServer::run()
{
while (true)
{
QByteArray datagram = "Broadcast message";
int bytes = udpSocket.writeDatagram(datagram.data(), datagram.size(), QHostAddress::LocalHost, 7755);
sleep(1);
}
}
To copy to clipboard, switch view to plain text mode
I can compile and start it and it does start sending out the "Boradcast message". However, it also prints the error message:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QUdpSocket(0x22ff48), parent's thread is QThread(0x3e3988), current thread is QThread(0x22ff40)
What does it exactly mean?
I tried moving the call to start() the UDP thread to the constructor of UDPTestServer, but that doesn't make a difference.
Thanks
Cruz
Bookmarks