PDA

View Full Version : Slot function not being executed in connect



Leirbag89
18th May 2011, 03:28
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>

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


UdpThread.cpp


#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");
}


main.cpp


#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();
}



Thanks for the help.

DanH
18th May 2011, 04:39
Well first, always test the result of connect():

bool success = connect(...);
Q_UNUSED(success);
Q_ASSERT(success);
But I don't think that's your problem in this case. More likely datagrams aren't being sent to the localhost you're referencing.

squidge
18th May 2011, 07:45
Your thread is causing far more complexity than you need as well and is not required.

Also, don't use a QPixmap in your thread. They are for main gui thread only.

Leirbag89
19th May 2011, 15:53
Thanks for the replies.

DanH: I did check if connect was working before and it was returning true I just didnt included in the code because it thought it would make it to messy.

squidge: I was using QPixmap so I could save one of the datagrams into a file to see if any were being received. If I remove this will that allow the SLOT function readPackages() to work? Thats the main issue I think.

Thanks

Added after 31 minutes:


More likely datagrams aren't being sent to the localhost you're referencing. -DanH

I also think this might be the problem. Is there a way to test if datagrams are being sent/received?

Santosh Reddy
19th May 2011, 16:33
Looks like, there is possiblity that the started() signal might have been emitted before making the connection.

so try statements in this order



// p->start(); // remove here
uThread->moveToThread(p);
QObject::connect(p, SIGNAL(started()), uThread, SLOT(run()));
p->start(); // move to here

Leirbag89
19th May 2011, 18:00
Looks like, there is possiblity that the started() signal might have been emitted before making the connection.

so try statements in this order



// p->start(); // remove here
uThread->moveToThread(p);
QObject::connect(p, SIGNAL(started()), uThread, SLOT(run()));
p->start(); // move to here


Tried... Failure... I am stumped

Thanks for replies

Santosh Reddy
19th May 2011, 18:10
Ok, then the only obvious reason, I can see the UPD socket is not receving any data packets, make sure data is really comming on to 127.0.0.1::5555

squidge
19th May 2011, 19:53
You do check the result code of udpSocket->bind I hope?

Have you confirmed the state of the socket?

If so, then you need to confirm data is arriving with a suitable network monitor.