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?
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;
}
Found it, toPlainText
work properly. Still don't know why toHtml doesn't.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With