Dmitriy Baranov
Dmitriy Baranov

Reputation: 163

Error while geting html code

I'm trying to get html code using QNetworkAccessManager, but it doesn't work. Result of reply in my program is site, but i need html. How can i convert it?

Widget::Widget(QWidget *pwgt): QWidget(pwgt)
{
   field = new QTextEdit(this);
   QNetworkReply *reply = manager.get(QNetworkRequest(QUrl("http://www.google.com")));
   QEventLoop loop;
   connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
   loop.exec();
   QString text = reply->read();
   field->setText(text);
}

Upvotes: 1

Views: 82

Answers (2)

Nejat
Nejat

Reputation: 32665

From the Qt documentation :

void QTextEdit::setText(const QString & text) [slot]

Sets the text edit's text. The text can be plain text or HTML and the text edit will try to guess the right format.

Use setHtml() or setPlainText() directly to avoid text edit's guessing.

You can use QTextEdit::setPlainText which sets the text editor's contents as plain text.

Upvotes: 1

Robert
Robert

Reputation: 1343

First of all do not try to make asynchronous code synchronous...

The problem could be that you're reading the content of the reply before the request could be finished. Connect the finished() signal with a slot (of your class) at try to read the data there (see http://qt-project.org/doc/qt-5/qnetworkaccessmanager.html)

Try also using readAll() for the QNetworkReply

Upvotes: 0

Related Questions