Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get website content from QWebEnginePage?

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?

like image 241
kajojeq Avatar asked Mar 20 '16 11:03

kajojeq


2 Answers

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;
}
like image 165
IAmInPLS Avatar answered Oct 29 '22 17:10

IAmInPLS


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

like image 2
kajojeq Avatar answered Oct 29 '22 17:10

kajojeq