Results 1 to 2 of 2

Thread: Multiple QNetworkAccessManager requests.

  1. #1
    Join Date
    Jul 2010
    Posts
    30
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Multiple QNetworkAccessManager requests.

    Hello QtCentre community. I wanted to make an application that would connect through proxy to several hosts and check which hosts have banished the proxy.
    At first I thought of making an UI in Qt and connection checks in cURL but then I found out that Qt has QNetworkAccessManager so I decided to use this instead.
    It actually works fine with one host - connects, waits for response, returns state. But if I run multiple checks from a loop it returns a state only once.
    I thought of executing next "get()" in callback function after the first check is done, but I don't think it's correct solution.
    Below I post my code, hopefully you guys can help me out with some solution.

    checker.h:
    Qt Code:
    1. QNetworkAccessManager *check[1024];
    To copy to clipboard, switch view to plain text mode 

    checker.cpp:
    Qt Code:
    1. void Checker::test()
    2. {
    3. int count = ui->hosts->topLevelItemCount();
    4. if(count == 0)
    5. {
    6. QMessageBox::information(this, "Info", "Hosts list is empty.");
    7. return;
    8. }
    9.  
    10. ui->progHosts->setMaximum(count);
    11. ui->progHosts->setValue(0);
    12.  
    13. for(int i = 0; i < count; ++i)
    14. {
    15. QTreeWidgetItem *item = ui->hosts->topLevelItem(i);
    16. check[i] = new QNetworkAccessManager;
    17. check[i]->setProxy(QNetworkProxy(QNetworkProxy::HttpProxy, "somerandomproxy.org", 80));
    18. check[i]->get(QNetworkRequest(QUrl(item->text(0))));
    19. connect(check[i], SIGNAL(finished(QNetworkReply *)), this, SLOT(testCallback(QNetworkReply *)));
    20. }
    21. }
    22.  
    23. void Checker::testCallback(QNetworkReply *reply)
    24. {
    25. QTreeWidgetItem *item = ui->hosts->topLevelItem(1);
    26. if(!item)
    27. return;
    28.  
    29. if(reply->error())
    30. {
    31. item->setText(2, reply->errorString());
    32. item->setTextColor(2, QColor(255, 0, 0));
    33. }
    34. else
    35. {
    36. item->setText(2, "OK");
    37. item->setTextColor(2, QColor(0, 255, 0));
    38. }
    39. }
    To copy to clipboard, switch view to plain text mode 

    Cheers, Diath.

    @Edit:
    Never mind, I've got it fixed by connection the finished() slot to a QNetworkReply.
    Last edited by Diath; 12th October 2011 at 22:35.

  2. #2
    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: Multiple QNetworkAccessManager requests.

    Note, you don't need separate network managers for each check, one is enough.
    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.


Similar Threads

  1. QNetworkAccessManager in multiple threads
    By mentalmushroom in forum Qt Programming
    Replies: 2
    Last Post: 7th August 2013, 23:14
  2. QNetworkAccessManager and multiple progress bars
    By bigjoeystud in forum Qt Programming
    Replies: 2
    Last Post: 2nd February 2011, 03:19
  3. Replies: 4
    Last Post: 19th January 2011, 13:49
  4. Tracking multiple requests with QNetwork
    By rbp in forum Qt Programming
    Replies: 4
    Last Post: 1st June 2010, 08:04
  5. QNetworkAccessManager can't handle multiple cookies?
    By krippy2k in forum Qt Programming
    Replies: 5
    Last Post: 7th June 2009, 23:12

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.