Results 1 to 4 of 4

Thread: How to wait till QNetworkReply gives a result

  1. #1
    Join Date
    Apr 2010
    Posts
    31
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Question How to wait till QNetworkReply gives a result

    Hello.
    Qt Code:
    1. QString text = selectedText();
    2. text = text.trimmed();
    3. text.insert(0, "http://www.");
    4. QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    5. QNetworkReply *reply = manager->head(QNetworkRequest(QUrl(text)));
    6. //QMessageBox::information(this,"asd","asdasd");
    7. QVariant variant = reply->header(QNetworkRequest::LocationHeader);
    8. if(!variant.isNull())
    9. {
    10. QString truncatedURL = text;
    11. if (text.length() > 18)
    12. {
    13. truncatedURL.truncate(15);
    14. truncatedURL += "...";
    15. }
    16.  
    17. //open selected text url in a new tab
    18. a = new KAction(KIcon("tab-new"), i18n("Open '%1' in New Tab", truncatedURL), this);
    19. a->setData(QUrl(text));
    20. connect(a, SIGNAL(triggered(bool)), this, SLOT(openLinkInNewTab()));
    21. menu.addAction(a);
    22.  
    23. //open selected text url in a new window
    24. a = new KAction(KIcon("window-new"), i18n("Open '%1' in New Window", truncatedURL), this);
    25. a->setData(QUrl(text));
    26. connect(a, SIGNAL(triggered(bool)), this, SLOT(openLinkInNewWindow()));
    27. menu.addAction(a);
    28.  
    29. menu.addSeparator();
    30. }
    To copy to clipboard, switch view to plain text mode 

    The purpose of the code is to check whether selected text is an URL to a web site or not. When I uncomment this line

    Qt Code:
    1. //QMessageBox::information(this,"asd","asdasd");
    To copy to clipboard, switch view to plain text mode 
    it works fine. But when I comment that line it doesn't! I think the application can have enough time to compute the QNetworkReply result. I don't know how to solve the problem, please help me!
    Thanks in advance!

  2. #2
    Join Date
    Nov 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to wait till QNetworkReply gives a result

    Replace
    Qt Code:
    1. //QMessageBox::information(this,"asd","asdasd");
    To copy to clipboard, switch view to plain text mode 
    with
    Qt Code:
    1. while (!reply->isFinished());
    To copy to clipboard, switch view to plain text mode 
    To wait while request is being processed.

    MT.

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

    Default Re: How to wait till QNetworkReply gives a result

    Quote Originally Posted by mac.tieu View Post
    Replace
    Qt Code:
    1. //QMessageBox::information(this,"asd","asdasd");
    To copy to clipboard, switch view to plain text mode 
    with
    Qt Code:
    1. while (!reply->isFinished());
    To copy to clipboard, switch view to plain text mode 
    To wait while request is being processed.

    MT.
    That will block the program.
    Instead, use signals and slots. http://doc.qt.nokia.com/4.7/qnetwork....html#finished

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: How to wait till QNetworkReply gives a result

    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. QRegExp - get only last result
    By kabanek in forum Newbie
    Replies: 2
    Last Post: 3rd November 2010, 22:17
  2. Replies: 5
    Last Post: 24th October 2010, 06:54
  3. Wait in thread till QTcp socket have some thing to read
    By hasnain in forum Qt Programming
    Replies: 2
    Last Post: 14th September 2010, 12:46
  4. Replies: 8
    Last Post: 27th October 2009, 09:07
  5. Replies: 3
    Last Post: 16th November 2006, 12:24

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.