Results 1 to 2 of 2

Thread: QThread and QNetworkAccessManger

  1. #1
    Join Date
    Feb 2010
    Posts
    99
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    31
    Thanked 3 Times in 3 Posts

    Default QThread and QNetworkAccessManger

    Hi,

    I am trying to create a worker thread which will get the html of links that are in string list.

    When I run the program I get following warning when the thread tries to connect finished() signal of QNetworkManager object.

    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QNetworkAccessManager(0x3aa790), parent's thread is QThread(0x3aa560), current thread is WorkerThread(0x28fe54)


    The code is as below
    Qt Code:
    1. //WorkerThread.h
    2.  
    3.  
    4. #include <QThread>
    5. #include <QStringList>
    6.  
    7. class QNetworkReply;
    8. class QNetworkAccessManager;
    9.  
    10. class WorkerThread : public QThread
    11. {
    12. Q_OBJECT
    13. public:
    14. WorkerThread();
    15. ~WorkerThread();
    16. void startLoadingWebPages(const QStringList& list);
    17. protected:
    18. virtual void run();
    19. private:
    20. QNetworkAccessManager *m_pNetworkAccessManager;
    21.  
    22. public slots:
    23. void replyFinished(QNetworkReply *);
    24.  
    25. };
    26.  
    27. //WorkerThread.cpp
    28. //Worker thread constructor
    29. WorkerThread::WorkerThread():
    30. {
    31. m_pNetworkAccessManager = new QNetworkAccessManager();
    32. connect(m_pNetworkAccessManager, SIGNAL(finished(QNetworkReply*)),
    33. this, SLOT(replyFinished(QNetworkReply*)));
    34. }
    35. void WorkerThread::startLoadingWebPages(const QStringList &links)
    36. {
    37. m_WebPageLinkList=links;
    38. start();
    39. }
    40.  
    41. void WorkerThread::run()
    42. {
    43. // QString link=m_WebPageLinkList.at(m_LinkIndex);
    44. foreach(QString string,m_WebPageLinkList)
    45. {
    46. qDebug(string.toAscii().data());
    47. m_pNetworkAccessManager->get(QNetworkRequest(QUrl(string)));
    48. }
    49. exec();
    50. }
    51.  
    52. void WorkerThread::replyFinished(QNetworkReply* pReply)
    53. {
    54. QString webData(pReply->readAll());
    55. qDebug(QString::number(webData.length()).toAscii().data());
    56. }
    To copy to clipboard, switch view to plain text mode 

    Can some one please help me out.
    Also when the slot replyFinished gets called the QNetworkReply->readAll() returns empty buffer.
    Last edited by dpatel; 24th October 2010 at 06:14.

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

    Default Re: QThread and QNetworkAccessManger

    The classic pitfall of QThread.

    Qt Code:
    1. m_pNetworkAccessManager->get(QNetworkRequest(QUrl(string)));
    To copy to clipboard, switch view to plain text mode 

    The QNetworkRequest and m_pNetworkAccessManager live in different threads.
    QThread by itself is not a new thread. A QThread object lives in the main thread. It only starts and manages a single new thread. Only the code in the run() function will be performed in a new thread.

    To make it yourself easy, do not subclass QThread. Leave it like it is.
    Instead, move all your code into a new QObject subclass. Then move the QObject object to the new QThread object using moveTothread.

    By the way, since the QNetwork classes can work asynchronously, you do not need to use threads. These classes will not block the main event loop.

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

    dpatel (24th October 2010)

Similar Threads

  1. Need help in Qthread
    By skumar434 in forum Qt Programming
    Replies: 1
    Last Post: 4th March 2009, 18:21
  2. Replies: 4
    Last Post: 26th June 2008, 19:41
  3. QThread in Qt4.4
    By ColonelMoW in forum Qt Programming
    Replies: 6
    Last Post: 2nd June 2008, 19:06
  4. Spawn a QThread in another QThread
    By david.corinex in forum Qt Programming
    Replies: 2
    Last Post: 19th December 2007, 13:54
  5. QThread
    By TheKedge in forum Qt Programming
    Replies: 8
    Last Post: 25th August 2006, 11:29

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.