How to get website content from QWebEnginePage?
Asked Answered
R

2

6

I installed the newest version of Qt (on Webkit, Qt5.2 had WTFcrash). I try to get content of my website when the page is loaded (and it is):

QString sHtml;
view.page()->toHtml([&](const QString& result){sHtml = result;qDebug() << result;});

But sHtml is empty, and debug not called. What am I doing wrong?

Relativistic answered 20/3, 2016 at 11:10 Comment(0)
R
2

Found it, toPlainText work properly. Still don't know why toHtml doesn't.

Relativistic answered 20/3, 2016 at 11:22 Comment(1)
Probably because sHtml went out of scope by the time toHtml was invoked.Obstetrics
D
4

You're not doing anything wrong, you're just calling an asynchronous function :

Asynchronous method to retrieve the page's content as HTML, enclosed in HTML and BODY tags. Upon successful completion, resultCallback is called with the page's content.

The HTML won't be available directly after the call to toHtml(). Instead, you can use some signals and slots to overcome this :

protected slots:
    void handleHTML(QString sHTML);

signals:
    void getHTML(QString sHTML);

 void yourClass::yourFunction()
 {
    connect(this, SIGNAL(getHTML(QString)), this, SLOT(handleHTML(QString)));
    view->page()->toHtml([this](const QString& result) mutable {emit getHTML(result);});
 }

void yourClass::handleHTML(QString sHTML)
{
      qDebug()<< "The HTML is :" << sHTML;
}
Dukey answered 20/6, 2016 at 10:22 Comment(0)
R
2

Found it, toPlainText work properly. Still don't know why toHtml doesn't.

Relativistic answered 20/3, 2016 at 11:22 Comment(1)
Probably because sHtml went out of scope by the time toHtml was invoked.Obstetrics

© 2022 - 2024 — McMap. All rights reserved.