Results 1 to 17 of 17

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.


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

    jan (2nd October 2009)

Similar Threads

  1. Replies: 6
    Last Post: 29th April 2009, 18:17
  2. QTcpSocket, QTcpServer problem
    By frido in forum Qt Programming
    Replies: 3
    Last Post: 3rd April 2009, 23:16
  3. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  4. Replies: 10
    Last Post: 20th March 2007, 22:19
  5. Main Thread (GUI Thread)
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 18th November 2006, 16: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
  •  
Qt is a trademark of The Qt Company.