Results 1 to 17 of 17

Thread: Creating a QTcpSocket or a QTcpServer in a thread other than the main thread.

  1. #1
    Join Date
    Sep 2009
    Posts
    9
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question Creating a QTcpSocket or a QTcpServer in a thread other than the main thread.

    I have discovered that QTcpSocket and QTcpServer has to be created in main thread in order to function properly. Created in other thread results in slots not being called. Unfortunately, my part was just writing a dll which does not know which thread is creating the class. Furthermore, running the class creation in main thread should not be a requirement. Is there a way to run them reliable in other threads? I have no ground to run network activities in a UI thread.

    Many Thanks!
    Last edited by jan; 29th September 2009 at 09:44. Reason: spelling error

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Creating a QTcpSocket or a QTcpServer in a thread other than the main thread.

    where have you read that?

    In Qt4 you can either:
    * work with blocking I/O (not in the gui thread, as otherwise your app freezes til the I/O is over)
    * work with non-blocking I/O: you need a QEventLoop running in your thread - you may use the main (i.e. gui) thread, but you do not have to.

    HTH

  3. The following user says thank you to caduel for this useful post:

    jan (2nd October 2009)

  4. #3
    Join Date
    May 2009
    Posts
    133
    Thanks
    10
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating a QTcpSocket or a QTcpServer in a thread other than the main thread.

    Quote Originally Posted by caduel View Post
    where have you read that?

    In Qt4 you can either:
    * work with blocking I/O (not in the gui thread, as otherwise your app freezes til the I/O is over)
    * work with non-blocking I/O: you need a QEventLoop running in your thread - you may use the main (i.e. gui) thread, but you do not have to.

    HTH
    Do you know how to use QNetworkAccessManager with QEventLoop other than the one in the main thread?

  5. The following user says thank you to piotr.dobrogost for this useful post:

    jan (2nd October 2009)

  6. #4
    Join Date
    Sep 2009
    Posts
    9
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Creating a QTcpSocket or a QTcpServer in a thread other than the main thread.

    Quote Originally Posted by caduel View Post
    where have you read that?

    In Qt4 you can either:
    * work with blocking I/O (not in the gui thread, as otherwise your app freezes til the I/O is over)
    * work with non-blocking I/O: you need a QEventLoop running in your thread - you may use the main (i.e. gui) thread, but you do not have to.

    HTH
    Here is a sample code to illustrate the problem I have encountered. I made it as brief as possible. Hence, it is not going with the best practice. Please forgive me for that.
    Qt Code:
    1. #include <QtNetwork/QTcpServer>
    2. #include <QtCore/QThread>
    3. #include <QtCore/QCoreApplication>
    4. #include <iostream>
    5. #include <assert.h>
    6.  
    7. class ServerThread : public QThread
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. ServerThread(QObject *ipParent)
    13. : QThread(ipParent)
    14. , mpServer(0)
    15. {}
    16.  
    17. void run ()
    18. {
    19. mpServer = new QTcpServer();
    20. bool lbOk(connect(mpServer, SIGNAL(newConnection()),
    21. this, SLOT(onNewConnection())));
    22. assert(lbOk);
    23. mpServer->listen(QHostAddress::Any, 8888);
    24. }
    25.  
    26. private:
    27. QTcpServer* mpServer;
    28.  
    29. protected slots:
    30. void onNewConnection()
    31. {
    32. std::cout << "new connection!" << std::endl;
    33. }
    34. };
    35.  
    36. int main(int argc, char *argv[])
    37. {
    38. QCoreApplication a(argc, argv);
    39. ServerThread lrThread(0);
    40. // lrThread.start();
    41. lrThread.run();
    42. return a.exec();
    43. }
    To copy to clipboard, switch view to plain text mode 

    It runs fine with expected outcome which print a message every time a connection is made.

    However, if the line 40 is uncommented and line 41 commented, the ServerThread::onNewConnection() method is never called with connections made.

    It stops working while the QTcpServer is not created on the main thread.

  7. #5
    Join Date
    Sep 2009
    Posts
    9
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Thumbs up Re: Creating a QTcpSocket or a QTcpServer in a thread other than the main thread.

    Quote Originally Posted by piotr.dobrogost View Post
    Do you know how to use QNetworkAccessManager with QEventLoop other than the one in the main thread?
    Thanks for the pointers. I will definitely look into that.

  8. #6
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Creating a QTcpSocket or a QTcpServer in a thread other than the main thread.

    you need to start your QThread's event loop, see QThread::exec()

  9. #7
    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: Creating a QTcpSocket or a QTcpServer in a thread other than the main thread.

    Quote Originally Posted by jan View Post
    I have discovered that QTcpSocket and QTcpServer has to be created in main thread in order to function properly.
    No, this is false. The socket has to have affinity with a thread with a running event loop to function properly or it needs to use the family of waitFor*() methods if not event loop is available.

    Quote Originally Posted by piotr.dobrogost View Post
    Do you know how to use QNetworkAccessManager with QEventLoop other than the one in the main thread?
    Qt Code:
    1. #include <QtGui>
    2. #include <QtNetwork>
    3.  
    4. class Thread : public QThread {
    5. Q_OBJECT
    6. public:
    7. Thread() : QThread(){ manager = 0; }
    8. ~Thread() { delete manager; }
    9. void run(){
    10. manager = new QNetworkAccessManager;
    11. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(processReply(QNetworkReply*)));
    12. QTimer::singleShot(0, this, SLOT(process()));
    13. exec();
    14. }
    15. public slots:
    16. void process(){
    17. QNetworkReply *reply = manager->get(QNetworkRequest(QUrl("http://www.qtcentre.org/")));
    18. QTimer::singleShot(5000, this, SLOT(process()));
    19. }
    20. void processReply(QNetworkReply *reply){
    21. qDebug() << reply->size();
    22. reply->deleteLater();
    23. }
    24. private:
    25. QNetworkAccessManager *manager;
    26. };
    27.  
    28. #include "main.moc"
    29.  
    30. int main(int argc, char **argv){
    31. QApplication app(argc, argv);
    32. Thread thread;
    33. thread.moveToThread(&thread);
    34. thread.start();
    35. return app.exec();
    36. }
    To copy to clipboard, switch view to plain text mode 
    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.


  10. The following user says thank you to wysota for this useful post:

    jan (2nd October 2009)

  11. #8
    Join Date
    May 2009
    Posts
    133
    Thanks
    10
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating a QTcpSocket or a QTcpServer in a thread other than the main thread.

    Is the purpose of this code to download the same page every 5 seconds?

  12. #9
    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: Creating a QTcpSocket or a QTcpServer in a thread other than the main thread.

    Yes. You wanted an example of QNetworkAccessManager working with a thread event loop, you didn't mention it should do something useful.
    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.


  13. #10
    Join Date
    Sep 2009
    Posts
    9
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Creating a QTcpSocket or a QTcpServer in a thread other than the main thread.

    Thanks for your sample. You make my day!

  14. #11
    Join Date
    May 2009
    Posts
    133
    Thanks
    10
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating a QTcpSocket or a QTcpServer in a thread other than the main thread.

    Would
    Qt Code:
    1. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(processReply(QNetworkReply*)), Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 
    work instead of
    Qt Code:
    1. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(processReply(QNetworkReply*)));
    2. QTimer::singleShot(0, this, SLOT(process()));
    To copy to clipboard, switch view to plain text mode 
    ?

  15. #12
    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: Creating a QTcpSocket or a QTcpServer in a thread other than the main thread.

    They are not equivalent. The timer causes the first request to be sent.
    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.


  16. The following user says thank you to wysota for this useful post:

    bob2oneil (10th March 2011)

  17. #13
    Join Date
    Oct 2009
    Location
    Rennes, France
    Posts
    20
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Re: Creating a QTcpSocket or a QTcpServer in a thread other than the main thread.

    Hi
    are your sure this it's safe?
    Quote Originally Posted by wysota View Post
    Qt Code:
    1. Thread thread;
    2. thread.moveToThread(&thread);
    3. thread.start();
    To copy to clipboard, switch view to plain text mode 
    You use moveToThread when the thread (which execute run) is not ever created
    But maybe i don't understand what do moveToThread and the signification of QObject thread affinity in Qt )
    Contributor from the French Qt community from developpez.com
    * Forum
    * FAQ Qt ( > 100 QR)
    * Advanced and Beginner Tutorials

  18. #14
    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: Creating a QTcpSocket or a QTcpServer in a thread other than the main thread.

    Quote Originally Posted by yan View Post
    Hi
    are your sure this it's safe?
    Yes.

    You use moveToThread when the thread (which execute run) is not ever created
    It doesn't matter, as far as I know. It's all about just marking which thread will handle events for the object and its children.
    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.


  19. #15
    Join Date
    Oct 2009
    Location
    Rennes, France
    Posts
    20
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Creating a QTcpSocket or a QTcpServer in a thread other than the main thread.

    Quote Originally Posted by wysota View Post
    It doesn't matter, as far as I know. It's all about just marking which thread will handle events for the object and its children.
    Thanks for confirmation.
    I investigate, and i understand all QObject have an affinity with a QThread and not the interfaced thread( that I believed), ans if you do
    Qt Code:
    1. thread.moveToThread(&thread);
    To copy to clipboard, switch view to plain text mode 
    you can't call start() by signal/slot system to start a thread because the QThread eventloop don't run.

    is right?
    Contributor from the French Qt community from developpez.com
    * Forum
    * FAQ Qt ( > 100 QR)
    * Advanced and Beginner Tutorials

  20. #16
    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: Creating a QTcpSocket or a QTcpServer in a thread other than the main thread.

    You can, but then you have to move the thread to itself after start() is executed - for example from a slot connected to the started() signal.
    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.


  21. The following user says thank you to wysota for this useful post:

    yan (7th October 2009)

  22. #17
    Join Date
    Oct 2009
    Location
    Rennes, France
    Posts
    20
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Creating a QTcpSocket or a QTcpServer in a thread other than the main thread.

    Quote Originally Posted by wysota View Post
    You can, but then you have to move the thread to itself after start() is executed - for example from a slot connected to the started() signal.
    ok thanks.
    sorry for except subject
    Contributor from the French Qt community from developpez.com
    * Forum
    * FAQ Qt ( > 100 QR)
    * Advanced and Beginner Tutorials

Similar Threads

  1. Replies: 6
    Last Post: 29th April 2009, 19:17
  2. QTcpSocket, QTcpServer problem
    By frido in forum Qt Programming
    Replies: 3
    Last Post: 4th April 2009, 00:16
  3. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 07:13
  4. Replies: 10
    Last Post: 20th March 2007, 23:19
  5. Main Thread (GUI Thread)
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 18th November 2006, 17:07

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.