QWebEnginePage: toHtml returns an empty string

I need to get some html from QWebEnginePage . I found the toHtml method in the documentation, but it always returns an empty string. I tried toPlainText and it works, but that is not what I need.

 MyClass::MyClass(QObject *parent) : QObject(parent) { _wp = new QWebEnginePage(); _wp->settings()->setAttribute(QWebEngineSettings::AutoLoadImages, false); _wp->settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, true); connect(_wp, SIGNAL(loadFinished(bool)), this, SLOT(wpLoadFinished(bool))); } void MyClass::start() { _wp->load(QUrl("http://google.com/")); } void MyClass::wpLoadFinished(bool s) { _wp->toHtml( [] (const QString &result) { qDebug()<<"html:"; qDebug()<<result; }); // return empty string /*_wp->toPlainText( [] (const QString &result) { qDebug()<<"txt:"; qDebug()<<result; });*/ //works perfectly } 

What am I doing wrong?

0
source share
2 answers

I get a head from QWebEngine. It's great. I have a job.

The lambada capture should be all that is "=", or "this" in the event of a signal. You will also need to “modify” to modify the captured copies. toHtml() , however, is asynchronous, so even if you capture html, it is unlikely to be available immediately after calling toHtml() in SomeFunction . You can overcome this by using a signal and a slot.

 protected slots: void handleHtml(QString sHtml); signals: void html(QString sHtml); void MainWindow::SomeFunction() { connect(this, SIGNAL(html(QString)), this, SLOT(handleHtml(QString))); view->page()->toHtml([this](const QString& result) mutable {emit html(result);}); } void MainWindow::handleHtml(QString sHtml) { qDebug()<<"myhtml"<< sHtml; } 
+2
source

I think that the problem is rather related to the connection problem. Your code works fine on my application:

  connect(page, SIGNAL(loadFinished(bool)), this, SLOT(pageLoadFinished(bool))); 

...

  page->load(QUrl("http://google.com/")); 

... loading time ...

  void MaClasse :: pageLoadFinished(bool s){ page->toHtml([this](const QString &result){ qDebug()<<"html:"; qDebug()<<result; item->setHtml(result);}); } 
0
source

All Articles