Qt – Getting source ( HTML code ) of a web page hosted on the internet

Issue

I want to get the source (HTML) of a webpage, for example the homepage of StackOverflow.

This is what I’ve coded so far:

QNetworkAccessManager manager;
QNetworkReply *response = manager.get(QNetworkRequest(QUrl(url)));

QString html = response->readAll(); // Source should be stored here

But nothing happens! When I try to get the value of the html string it’s empty (“”).

So, what to do? I am using Qt 5.3.1.

Solution

You have to add QEventLoop between.

QNetworkAccessManager manager;
QNetworkReply *response = manager.get(QNetworkRequest(QUrl(url)));
QEventLoop event;
connect(response,SIGNAL(finished()),&event,SLOT(quit()));
event.exec();
QString html = response->readAll(); // Source should be stored here

Answered By – MKAROL

Answer Checked By – Robin (AngularFixing Admin)

Leave a Reply

Your email address will not be published.