Results 1 to 6 of 6

Thread: QNetworkAccessManager does not work from QRunnable

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

    Default QNetworkAccessManager does not work from QRunnable

    Hello

    I have the following code:

    Below is a Worker class which is QRunnable object. As you know, it is started from QThreadPool.
    Qt Code:
    1. class Worker : public QObject, public QRunnable
    2. {
    3. Q_OBJECT
    4. public:
    5. Worker();
    6.  
    7. protected:
    8. virtual void run();
    9. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void Worker::run()
    2. {
    3. Sender sender;
    4. sender.send({"text1", "text2"});
    5. }
    To copy to clipboard, switch view to plain text mode 

    The sender.send() method:
    Qt Code:
    1. void DataSender::send(const QList<QString>& data)
    2. {
    3. QUrl serviceUrl = QUrl(given_ip_addr);
    4. QNetworkRequest request(serviceUrl);
    5. QJsonObject json;
    6. for (auto& line : data)
    7. json.insert("data", line);
    8. QJsonDocument jsonDoc(json);
    9. QByteArray jsonData= jsonDoc.toJson();
    10. request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
    11. request.setHeader(QNetworkRequest::ContentLengthHeader,QByteArray::number(jsonData.size()));
    12. QNetworkReply *reply = _networkMgr.post(request, jsonData);
    13. }
    To copy to clipboard, switch view to plain text mode 

    Generally, the problem is: there is no any messages send to the endpoint given_ip_addr. No messages are received by the endpoint.

    I think, the problem is in the running QNetworkAccessManager in the new thread, because I have tested another solution - I ran exactly the same send method in MAIN thread of whole application and there weren't any problems.

    Do you have any idea? Unfortunately, I did not find anything in google about such problem.

    Best Regards!

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,199
    Thanks
    237
    Thanked 668 Times in 658 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QNetworkAccessManager does not work from QRunnable

    Every variable you define in your send() method is allocated on the stack, and thus will go out of scope as soon as the function exits. This includes the pointer to the QNetworkReply instance and your json data. From the documentation for QNetworkAccessManager::post():

    data must be open for reading and must remain valid until the finished() signal is emitted for this reply.
    Post operations are processed asynchronously, so you can't define your data as a variable that will go out of scope before the request completes.

    You should spend some time trying to understand the documentation for QNetworkAccessManager and the examples and tutorials supplied with your Qt distribution. It is clear from your posts and the questions you are asking that you do not understand how this class should be used.
    <=== 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.

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

    Default Re: QNetworkAccessManager does not work from QRunnable

    Quote Originally Posted by d_stranz View Post
    Every variable you define in your send() method is allocated on the stack, and thus will go out of scope as soon as the function exits. This includes the pointer to the QNetworkReply instance and your json data. From the documentation for QNetworkAccessManager::post():



    Post operations are processed asynchronously, so you can't define your data as a variable that will go out of scope before the request completes.

    You should spend some time trying to understand the documentation for QNetworkAccessManager and the examples and tutorials supplied with your Qt distribution. It is clear from your posts and the questions you are asking that you do not understand how this class should be used.
    Ok, so if it so clear for you, run such code in your IDE and add some QThread::sleep at the end of run() method. Of course, calling sender.send() code is not the only one in run() method and using sleep() method is does not help to resolve this issue. Are my arguments correct?

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

    Default Re: QNetworkAccessManager does not work from QRunnable

    I meant that adding sleep, checks the network manager variable and it is ok during sending and waiting for QNetworkReply. Unfortunately, it still does not work.

  5. #5
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    461
    Thanked 64 Times in 60 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager does not work from QRunnable

    Quote Originally Posted by d_stranz View Post
    Every variable you define in your send() method is allocated on the stack, and thus will go out of scope as soon as the function exits.
    It's even better - take a look at the run() method...

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,199
    Thanks
    237
    Thanked 668 Times in 658 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QNetworkAccessManager does not work from QRunnable

    It's even better - take a look at the run() method...
    Yes, saw that. But one step at a time. Eventually the OP will run out of hammers and might start to think about the problem and the approach.
    Last edited by d_stranz; 25th October 2019 at 19:26.
    <=== 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 and QRunnable issue
    By luk_man_ in forum Qt Programming
    Replies: 7
    Last Post: 23rd October 2019, 17:57
  2. get status of QRunnable
    By Qtonimo in forum Qt Programming
    Replies: 6
    Last Post: 30th July 2012, 09:13
  3. Use QRunnable without QThreadPool
    By Qiieha in forum Qt Programming
    Replies: 2
    Last Post: 18th August 2011, 11:02
  4. QNetworkAccessManager doesn't work
    By Floppy in forum Newbie
    Replies: 7
    Last Post: 14th November 2009, 17:32
  5. QThreadPool and QRunnable
    By jimc1200 in forum Qt Programming
    Replies: 3
    Last Post: 6th May 2009, 11:43

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.