PDA

View Full Version : How to wait till QNetworkReply gives a result



Furkan
22nd November 2010, 20:12
Hello.

QString text = selectedText();
text = text.trimmed();
text.insert(0, "http://www.");
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QNetworkReply *reply = manager->head(QNetworkRequest(QUrl(text)));
//QMessageBox::information(this,"asd","asdasd");
QVariant variant = reply->header(QNetworkRequest::LocationHeader);
if(!variant.isNull())
{
QString truncatedURL = text;
if (text.length() > 18)
{
truncatedURL.truncate(15);
truncatedURL += "...";
}

//open selected text url in a new tab
a = new KAction(KIcon("tab-new"), i18n("Open '%1' in New Tab", truncatedURL), this);
a->setData(QUrl(text));
connect(a, SIGNAL(triggered(bool)), this, SLOT(openLinkInNewTab()));
menu.addAction(a);

//open selected text url in a new window
a = new KAction(KIcon("window-new"), i18n("Open '%1' in New Window", truncatedURL), this);
a->setData(QUrl(text));
connect(a, SIGNAL(triggered(bool)), this, SLOT(openLinkInNewWindow()));
menu.addAction(a);

menu.addSeparator();
}

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


//QMessageBox::information(this,"asd","asdasd");
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!

mac.tieu
23rd November 2010, 01:53
Replace

//QMessageBox::information(this,"asd","asdasd");
with

while (!reply->isFinished());
To wait while request is being processed.

MT.

tbscope
23rd November 2010, 04:23
Replace

//QMessageBox::information(this,"asd","asdasd");
with

while (!reply->isFinished());
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/qnetworkaccessmanager.html#finished

wysota
23rd November 2010, 10:00
Or QEventLoop.