QWebEngineCallback? &resultCallback? How does it work?
-
Not understanding callback for QWebEngineCallback as a parameter for toHtml() which appear to be...
const QWebEngineCallback<const QString &> &resultCallback
I am using toHtml like so ...
QWebEngineView *myWeb = new QWebEngineView();
myWeb->load(QUrl("https://www.some-site-here.com/"));
myWeb->page()->toHtml(what parameter here?)
How might I get a QString from toHtml? What does this callback function look like? Where does
&resultCallback
come from?
-
@desun_evan
As https://doc.qt.io/qt-6/qwebenginepage.html#toHtml says:Note: resultCallback can be any of a function pointer, a functor or a lambda, and it is expected to take a QString parameter.
So you just pass it a function/lambda you write, which takes a parameter of a
QString
. A full example using a lambda, and discussion about the required scope of any variable if you wish to save for future useQString
parameter passed to receive the HTML from the web page, is given in the solution at https://stackoverflow.com/questions/45363190/get-html-from-qwebenginepage-in-qwebengineview-using-lamda:// we are in class `MyClass` // next line is member variable, so its lifetime persists QString m_html; qDebug() << "1. About to call QWebEnginePage::toHtml()" m_page.toHtml([this](QString html) { qDebug() << "3. HTML now retrieved from page" << html; // if you want to store the `html` parameter received, put it in a persistent class variable this->m_html = html; // if you want to know when this has been called and completed, emit your own signal // emit htmlReceived(); }); qDebug() << "2. Called QWebEnginePage::toHtml(), but result not yet generated/received" // if you need to block to await the HTML generation before your code proceeds, use a `QEventLoop` QEventLoop loop; QObject::connect(this, &MyClass::htmlReceived, &loop, &QEventLoop::quit);
Note how the expected order of the
qDebug()
statements is given by the1
,2
,3
. -