Hello
I am new here. Being looking everywhere to see if I could find an answer to my problem. So here is my problem: I am trying to make a UDP Connection and receive a video stream from a port. I have been referencing to the Qt examples with QUdpSockets and such. The problem is that the program does not seem to be going into the class slot function when I make a connection. I also have to implement this UDP connection using threads because I need to be able to constantly receive the live stream video while doing other stuff in a GUI.
Can anyone please help??
Here is my code:
UdpThread.h
#ifndef UDPTHREAD
#define UDPTHREAD
#include <QtCore>
#include <QHostAddress>
#include <QUdpSocket>
#include <QByteArray>
#include <QPixmap>
#include <QObject>
#include <QBuffer>
#include <QFile>
#include <QThread>
{
Q_OBJECT
public:
UdpThread();
public slots:
void run();
private:
private slots:
void readPackages();
};
#endif
#ifndef UDPTHREAD
#define UDPTHREAD
#include <QtCore>
#include <QHostAddress>
#include <QUdpSocket>
#include <QByteArray>
#include <QPixmap>
#include <QObject>
#include <QBuffer>
#include <QFile>
#include <QThread>
class QUdpSocket;
class UdpThread : public QObject
{
Q_OBJECT
public:
UdpThread();
public slots:
void run();
private:
QUdpSocket *udpSocket;
void processTheDatagram(QByteArray);
private slots:
void readPackages();
};
#endif
To copy to clipboard, switch view to plain text mode
UdpThread.cpp
#include <QtGui>
#include <QtNetwork>
#include "UdpThread.h"
//Constructor
UdpThread::UdpThread()
{
}
void UdpThread::run()
{
printf("run test\n");
printf("testing 1 more time\n");
//SLOT(readPackages()) Does not get executed
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPackages()), Qt::DirectConnection);
}
//reading data from the port
void UdpThread::readPackages()
{
printf("before test\n"); //Never gets printed or called!
while(udpSocket->hasPendingDatagrams())
{
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(), datagram.size()/**, &sender, &senderPort**/);
printf("in while test\n");
processTheDatagram(datagram);
}
}
//display the data into an image
void UdpThread
::processTheDatagram(QByteArray qba
) {
image.loadFromData(qba);
image.save("test", "JPG");
printf("file test\n");
}
#include <QtGui>
#include <QtNetwork>
#include "UdpThread.h"
//Constructor
UdpThread::UdpThread()
{
}
void UdpThread::run()
{
printf("run test\n");
udpSocket = new QUdpSocket(this);
printf("testing 1 more time\n");
udpSocket->bind(QHostAddress::LocalHost, 5555);
//SLOT(readPackages()) Does not get executed
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPackages()), Qt::DirectConnection);
}
//reading data from the port
void UdpThread::readPackages()
{
printf("before test\n"); //Never gets printed or called!
while(udpSocket->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(), datagram.size()/**, &sender, &senderPort**/);
printf("in while test\n");
processTheDatagram(datagram);
}
}
//display the data into an image
void UdpThread::processTheDatagram(QByteArray qba)
{
QPixmap image;
image.loadFromData(qba);
image.save("test", "JPG");
printf("file test\n");
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include <QApplication>
#include "UdpThread.h"
int main(int argc, char **argv)
{
UdpThread *uThread = new UdpThread;
p->start();
uThread->moveToThread(p);
QObject::connect(p,
SIGNAL(started
()), uThread,
SLOT(run
()));
return app.exec();
}
#include <QApplication>
#include "UdpThread.h"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QThread *p = new QThread;
UdpThread *uThread = new UdpThread;
p->start();
uThread->moveToThread(p);
QObject::connect(p, SIGNAL(started()), uThread, SLOT(run()));
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Thanks for the help.
Bookmarks