Results 1 to 10 of 10

Thread: QTcpSocket no signal when using in a thread

  1. #1
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default QTcpSocket no signal when using in a thread

    Hello. I am trying to use non-blocking sockets inside a thread, but I don't receive connected signal. Below is my code and method socketConnected is never get called.

    Qt Code:
    1. class Downloader: public QThread
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. Downloader(QList<Segment> &segments):segments(segments)
    7. {
    8. bool con;
    9. con = connect(&sock, SIGNAL(connected()), this, SLOT(socketConnected()));
    10. sock.connectToHost("myhost",119);
    11. }
    12.  
    13. void run()
    14. {
    15. exec();
    16. }
    17.  
    18.  
    19. ~Downloader()
    20. {
    21. }
    22.  
    23. protected slots:
    24.  
    25. void socketConnected()
    26. {
    27. // do something
    28.  
    29. }
    30.  
    31.  
    32.  
    33. protected:
    34. QList<Segment> &segments;
    35. QTcpSocket sock;
    36.  
    37. };
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpSocket no signal when using in a thread

    Please search the forum (or the web) for "qt you're doing it wrong". If that's not enough search the forum for thread+socket.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QTcpSocket no signal when using in a thread

    I found this page for the mentioned request: http://labs.qt.nokia.com/2010/06/17/...oing-it-wrong/
    They say about threads affinity and the need to subclass QThread. Is there something wrong with the thread affinity?

    I also tried to do it this way, but still doesn't work:
    Qt Code:
    1. class Downloader: public QThread
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. Downloader(QList<Segment> &segments):segments(segments)
    7. {
    8. }
    9.  
    10. void run()
    11. {
    12. QTcpSocket sock;
    13. bool con = connect(&sock, SIGNAL(connected()), this, SLOT(socketConnected()));
    14. sock.connectToHost("myhost",119);
    15.  
    16. exec();
    17. }
    18.  
    19.  
    20. ~Downloader()
    21. {
    22. }
    23.  
    24. protected slots:
    25.  
    26. void socketConnected()
    27. {
    28. // never gets called
    29. }
    30.  
    31.  
    32. protected:
    33. QList<Segment> &segments;
    34. };
    To copy to clipboard, switch view to plain text mode 

    I know subclassing QThread is not necessary, but this approach is more convenient for me: no need to create two objects (Worker and QThread) , no need to establish extra connections (just call wait).

    I don't understand what is wrong with the code.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpSocket no signal when using in a thread

    You have to handle signals for your socket in the same thread as the affinity of the socket. Furthermore an event loop needs to be running in this thread.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QTcpSocket no signal when using in a thread

    Ok, thank you for the explanation. I've just tried the following code. It doesn't work if I call QThread::wait for the thread where worker is running, and that seems to be strange, because that thread must call exec and process all events.

    Qt Code:
    1. // Worker declaration (worker.h)
    2. class Worker : public QObject
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. Worker();
    8. ~Worker();
    9.  
    10. signals:
    11. void finished();
    12.  
    13. public slots:
    14.  
    15. void start();
    16. void socketConnected();
    17. void socketError();
    18. void socketDisconnected();
    19.  
    20. private:
    21. QTcpSocket sock;
    22. };
    23.  
    24. Q_DECLARE_METATYPE(Worker*)
    25.  
    26.  
    27.  
    28. // usage (main.cpp)
    29. int main(int argc, char *argv[])
    30. {
    31. QCoreApplication a(argc, argv);
    32.  
    33. qRegisterMetaType<Worker*>();
    34.  
    35. QThread *t = new QThread;
    36. Worker *w = new Worker;
    37. w->moveToThread(t);
    38. bool con;
    39. con = QObject::connect(t, SIGNAL(started()), w, SLOT(start()));
    40. con = QObject::connect(w, SIGNAL(finished()), t, SLOT(quit()));
    41.  
    42. t->start();
    43. t->wait(); // !!! this causes a problem
    44.  
    45. return a.exec();
    46. }
    To copy to clipboard, switch view to plain text mode 

    As far as I see event processing in the worker's thread doesn't affect anything, but the 'outer' thread does (here it is the main thread). I tried to change the code and place it inside of the other thread:

    Qt Code:
    1. // this is how outer thread works
    2. void MyThread::run()
    3. {
    4. qRegisterMetaType<Worker*>();
    5.  
    6. QThread *t = new QThread;
    7. Worker *w = new Worker;
    8. w->moveToThread(t);
    9. bool con;
    10. con = QObject::connect(t, SIGNAL(started()), w, SLOT(start()));
    11. con = QObject::connect(w, SIGNAL(finished()), t, SLOT(quit()));
    12. con = QObject::connect(t, SIGNAL(finished()), this, SLOT(quit()));
    13. t->start();
    14.  
    15. exec();
    16. }
    17.  
    18. // and this is how MyThread is used
    19. int main(int argc, char *argv[])
    20. {
    21. QCoreApplication a(argc, argv);
    22.  
    23. MyThread *t = new MyThread;
    24. t->start();
    25. t->wait();
    26.  
    27. return a.exec();
    28. }
    To copy to clipboard, switch view to plain text mode 

    The last code works, I think because of QThread::exec called inside MyThread::run. But I thought that event processing must be performed by that thread where I moved my Worker object (called t in my case). Could you clarify this, please?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpSocket no signal when using in a thread

    Consider what is the thread affinity of the socket in your first snippet in the previous post.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QTcpSocket no signal when using in a thread

    I call w->moveToThread(t), this should push worker object (along with all its members like sock) to the thread t. As far as I know, by default t performs event processing in its run method simply calling exec. So I have QTcpSocket that belongs to the thread t, event processing in the thread t, and signals processed by the slots of Worker object that also belongs to the thread t. What is wrong with socket affinity?
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication a(argc, argv);
    4.  
    5. qRegisterMetaType<Worker*>();
    6.  
    7. QThread *t = new QThread;
    8. Worker *w = new Worker;
    9. w->moveToThread(t);
    10. bool con;
    11. con = QObject::connect(t, SIGNAL(started()), w, SLOT(start()));
    12. con = QObject::connect(w, SIGNAL(finished()), t, SLOT(quit()));
    13.  
    14. t->start();
    15. t->wait(); // !!! this causes a problem, if i remove this line the code will work
    16.  
    17. return a.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

    Worker declaration:
    Qt Code:
    1. class Worker : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. Worker();
    7. ~Worker();
    8.  
    9. signals:
    10. void finished();
    11.  
    12. public slots:
    13.  
    14. void start();
    15. void socketConnected();
    16. void socketError();
    17. void socketDisconnected();
    18.  
    19. private:
    20. QTcpSocket sock;
    21. };
    22.  
    23. Q_DECLARE_METATYPE(Worker*)
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpSocket no signal when using in a thread

    Tell me what is the purpose of using this thread if you immediately halt the main thread. Why not perform all the work in the main thread then?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QTcpSocket no signal when using in a thread

    It is just a test example (I wanted to make it simpler). I need to perform some network operation using several non-blocking sockets, and I need to be able to wait until the thread performing this operations is finished (it is not the only task in the project).

  10. #10
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QTcpSocket no signal when using in a thread

    When you do not use blocking sockets, there is no need to use a thread.

    Your code will become much much more simple if you do not use threads.

    I'm not sure what happens in your worker::start function, but if you already make some connections there to another client and wait for some data to arrive, halting the thread is indeed going to give you some problems. You should implement proper logic to handle this.
    Which in turn means that it is much more easy to not use threads at all for this.

Similar Threads

  1. QTcpSocket deamon-thread
    By mihau in forum Qt Programming
    Replies: 8
    Last Post: 18th March 2011, 07:58
  2. QTcpSocket EventLoop thread is still running?
    By mihau in forum Qt Programming
    Replies: 0
    Last Post: 28th February 2011, 09:41
  3. Replies: 9
    Last Post: 28th November 2009, 20:31
  4. Replies: 16
    Last Post: 7th October 2009, 08:17
  5. is qtcpsocket a thread
    By ersin.ozkan in forum Qt Programming
    Replies: 1
    Last Post: 11th March 2009, 09:39

Tags for this Thread

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.