Results 1 to 8 of 8

Thread: QNetworkAccessManager and QRunnable issue

  1. #1
    Join Date
    May 2016
    Posts
    10
    Qt products
    Qt5

    Default QNetworkAccessManager and QRunnable issue

    Hello,

    I have the such code architecture. For each request I am creating Worker class, which is QRunnable object and it should be started in a new QThread by QThreadPool.
    This Worker has the following code:

    Qt Code:
    1. #include "Worker.h"
    2. #include <QString>
    3.  
    4. Worker::Worker(const WorkerData& data) :
    5. _reader()
    6. _writer(),
    7. _sender()
    8. {
    9. }
    10.  
    11. void Worker::run()
    12. {
    13. _reader.readFile();
    14. }
    15.  
    16. void Worker::sendData(const DataToSend& data)
    17. {
    18. _writer.write(data);
    19. _sender.send(data);
    20. }
    To copy to clipboard, switch view to plain text mode 

    The Sender header code:
    Qt Code:
    1. #include <QObject>
    2. #include <QtNetwork/QNetworkAccessManager>
    3.  
    4. class DataSender : public QObject
    5. {
    6. Q_OBJECT
    7. public:
    8. DataSender();
    9.  
    10. void send(const DataToSend& data);
    11.  
    12. private:
    13. QNetworkAccessManager _networkMgr;
    14. };
    To copy to clipboard, switch view to plain text mode 

    The Sender.cpp code:
    Qt Code:
    1. #include <QUrl>
    2. #include <QJsonObject>
    3. #include <QJsonDocument>
    4. #include <QtNetwork/QNetworkReply>
    5. DataSender::DataSender() : _networkMgr()
    6. {
    7.  
    8. }
    9.  
    10. void DataSender::send(const DataToSend& data)
    11. {
    12. qDebug() << "\tINFO: [DataSender::send] sending data to endpoint: " << data;
    13.  
    14. QUrl serviceUrl = QUrl("http://someUrl.com");
    15. QNetworkRequest request(serviceUrl);
    16. QJsonObject json;
    17. json.insert("data", data.first());
    18. QJsonDocument jsonDoc(json);
    19. QByteArray jsonData= jsonDoc.toJson();
    20. request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
    21. request.setHeader(QNetworkRequest::ContentLengthHeader,QByteArray::number(jsonData.size()));
    22. QNetworkReply *reply = _networkMgr.post(request, jsonData);
    23. }
    To copy to clipboard, switch view to plain text mode 

    As you know, the only run() method in QRunnable object is running in a new thread. Based on that, all my QNetworkAccessManager is created in the old thread, because it is not created in run method, am I right?

    I am asking about that, because i got the following error:

    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QNetworkAccessManager(0xda5a74c), parent's thread is QThread(0xdb14e0), current thread is QThread(0xda579a0)
    Do you have any idea why I get this error?
    I do not see the cause of the problem...

  2. #2
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    452
    Thanked 62 Times in 58 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager and QRunnable issue

    Why do you want to create the request in a separate thread at all? It's not needed: https://doc.qt.io/qt-5/qnetworkacces...r.html#details

  3. #3
    Join Date
    May 2016
    Posts
    10
    Qt products
    Qt5

    Default Re: QNetworkAccessManager and QRunnable issue

    Quote Originally Posted by ChristianEhrlicher View Post
    Why do you want to create the request in a separate thread at all? It's not needed: https://doc.qt.io/qt-5/qnetworkacces...r.html#details
    ..because there will be a lot of requests to my main app and I would like to dispatch them to many threads, thus I use QThreadPool with Workers for each request.

  4. #4
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    452
    Thanked 62 Times in 58 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager and QRunnable issue

    So did you actually read the link? Then you would see that your idea will not work.

  5. #5
    Join Date
    May 2016
    Posts
    10
    Qt products
    Qt5

    Default Re: QNetworkAccessManager and QRunnable issue

    Quote Originally Posted by ChristianEhrlicher View Post
    So did you actually read the link? Then you would see that your idea will not work.
    Yes, I saw it. Network manager must be run in the same thread it was created. Based on that I create it in worker, but Worker itself is not in the new thread. In the new thread is Worker::run() method only, am I right?

  6. #6
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    452
    Thanked 62 Times in 58 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager and QRunnable issue

    I meant this:

    "Note: QNetworkAccessManager queues the requests it receives. The number of requests executed in parallel is dependent on the protocol. Currently, for the HTTP protocol on desktop platforms, 6 requests are executed in parallel for one host/port combination."

    So there is no real need to create a thread and when then you need to create on QNAM per thread.

  7. #7
    Join Date
    May 2016
    Posts
    10
    Qt products
    Qt5

    Default Re: QNetworkAccessManager and QRunnable issue

    Quote Originally Posted by ChristianEhrlicher View Post
    I meant this:

    "Note: QNetworkAccessManager queues the requests it receives. The number of requests executed in parallel is dependent on the protocol. Currently, for the HTTP protocol on desktop platforms, 6 requests are executed in parallel for one host/port combination."

    So there is no real need to create a thread and when then you need to create on QNAM per thread.
    I see, but presence of QNAM is not the main factor to using QThreadPool class. Each request processes files (read/write one file) and based on that the application architecture is better and more flexible.

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,167
    Thanks
    235
    Thanked 662 Times in 652 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QNetworkAccessManager and QRunnable issue

    based on that the application architecture is better and more flexible.
    I have a square peg. I do not care if the hole is round. I will make it fit. I just need to use a bigger hammer.

    Maybe you should consider threading the file processing after the network requests are complete, and not combine the two and attempt to thread the whole thing.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QNetworkAccessManager proxy issue
    By dholliday in forum Qt Programming
    Replies: 13
    Last Post: 8th May 2019, 11:20
  2. QNetworkAccessManager issue on Qt 4.8.7
    By AndyBrice in forum Qt Programming
    Replies: 2
    Last Post: 5th October 2015, 11:04
  3. Replies: 3
    Last Post: 12th September 2015, 20:13
  4. Replies: 3
    Last Post: 27th June 2015, 05:00
  5. QWebPluginFactory::create issue with QNetworkAccessManager
    By brcontainer in forum Qt Programming
    Replies: 0
    Last Post: 15th December 2013, 20:32

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.