Results 1 to 8 of 8

Thread: Slot function not being executed in connect

  1. #1
    Join Date
    May 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Slot function not being executed in connect

    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
    Qt Code:
    1. #ifndef UDPTHREAD
    2. #define UDPTHREAD
    3.  
    4. #include <QtCore>
    5. #include <QHostAddress>
    6. #include <QUdpSocket>
    7. #include <QByteArray>
    8. #include <QPixmap>
    9. #include <QObject>
    10. #include <QBuffer>
    11. #include <QFile>
    12. #include <QThread>
    13.  
    14. class QUdpSocket;
    15.  
    16. class UdpThread : public QObject
    17. {
    18. Q_OBJECT
    19. public:
    20. UdpThread();
    21.  
    22. public slots:
    23. void run();
    24.  
    25. private:
    26. QUdpSocket *udpSocket;
    27. void processTheDatagram(QByteArray);
    28.  
    29. private slots:
    30. void readPackages();
    31.  
    32. };
    33. #endif
    To copy to clipboard, switch view to plain text mode 

    UdpThread.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include <QtNetwork>
    3. #include "UdpThread.h"
    4.  
    5. //Constructor
    6. UdpThread::UdpThread()
    7. {
    8.  
    9. }
    10.  
    11. void UdpThread::run()
    12. {
    13. printf("run test\n");
    14. udpSocket = new QUdpSocket(this);
    15. printf("testing 1 more time\n");
    16.  
    17. udpSocket->bind(QHostAddress::LocalHost, 5555);
    18.  
    19. //SLOT(readPackages()) Does not get executed
    20. connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPackages()), Qt::DirectConnection);
    21. }
    22.  
    23. //reading data from the port
    24. void UdpThread::readPackages()
    25. {
    26. printf("before test\n"); //Never gets printed or called!
    27. while(udpSocket->hasPendingDatagrams())
    28. {
    29. QByteArray datagram;
    30. datagram.resize(udpSocket->pendingDatagramSize());
    31. udpSocket->readDatagram(datagram.data(), datagram.size()/**, &sender, &senderPort**/);
    32. printf("in while test\n");
    33. processTheDatagram(datagram);
    34. }
    35. }
    36.  
    37. //display the data into an image
    38. void UdpThread::processTheDatagram(QByteArray qba)
    39. {
    40. QPixmap image;
    41. image.loadFromData(qba);
    42. image.save("test", "JPG");
    43. printf("file test\n");
    44. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include "UdpThread.h"
    4.  
    5. int main(int argc, char **argv)
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. QThread *p = new QThread;
    10. UdpThread *uThread = new UdpThread;
    11. p->start();
    12. uThread->moveToThread(p);
    13. QObject::connect(p, SIGNAL(started()), uThread, SLOT(run()));
    14. return app.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 


    Thanks for the help.

  2. #2
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Slot function not being executed in connect

    Well first, always test the result of connect():
    Qt Code:
    1. bool success = connect(...);
    2. Q_UNUSED(success);
    3. Q_ASSERT(success);
    To copy to clipboard, switch view to plain text mode 
    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.

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Slot function not being executed in connect

    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.

  4. #4
    Join Date
    May 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Slot function not being executed in connect

    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?
    Last edited by Leirbag89; 19th May 2011 at 15:53.

  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Slot function not being executed in connect

    Looks like, there is possiblity that the started() signal might have been emitted before making the connection.

    so try statements in this order

    Qt Code:
    1. // p->start(); // remove here
    2. uThread->moveToThread(p);
    3. QObject::connect(p, SIGNAL(started()), uThread, SLOT(run()));
    4. p->start(); // move to here
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    May 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Slot function not being executed in connect

    Quote Originally Posted by Santosh Reddy View Post
    Looks like, there is possiblity that the started() signal might have been emitted before making the connection.

    so try statements in this order

    Qt Code:
    1. // p->start(); // remove here
    2. uThread->moveToThread(p);
    3. QObject::connect(p, SIGNAL(started()), uThread, SLOT(run()));
    4. p->start(); // move to here
    To copy to clipboard, switch view to plain text mode 
    Tried... Failure... I am stumped

    Thanks for replies

  7. #7
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Slot function not being executed in connect

    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

  8. #8
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Slot function not being executed in connect

    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.

Similar Threads

  1. Connect slot to an int function !?
    By hakermania in forum Newbie
    Replies: 23
    Last Post: 30th December 2010, 12:08
  2. Can't connect a signal to a slot
    By cejohnsonsr in forum Newbie
    Replies: 5
    Last Post: 26th August 2010, 20:42
  3. QThread slot executed in GUI thread
    By tnyblom in forum Qt Programming
    Replies: 13
    Last Post: 25th May 2010, 07:49
  4. connect a qpushbutton a slot
    By Lycus HackerEmo in forum Newbie
    Replies: 13
    Last Post: 29th March 2010, 09:14
  5. Qt Designer & Qt4, connect to my own slot.
    By geitosten in forum Newbie
    Replies: 2
    Last Post: 17th February 2007, 19:22

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.