我需要从QWebEnginePage中检索一些html。我在文档中找到了方法toHtml,但它总是返回一个空字符串。我试过toPlainText,它可以工作,但这不是我所需要的。
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
}我做错了什么?
发布于 2016-06-07 23:27:41
我在QWebEngine周围转来转去。它很酷。我有以下工作要做。
在发出信号的情况下,lambada捕获必须是"=“或"this”。您还需要“可变”来修改捕获的副本。然而,toHtml()是异步的,所以即使捕获了html,在调用toHtml() in SomeFunction之后,它也不太可能直接可用。您可以通过使用信号和插槽来克服这一点。
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;
}发布于 2016-08-12 14:58:19
我认为这个问题更多的是一个连接问题。您的代码在我的应用程序上运行得很好:
connect(page, SIGNAL(loadFinished(bool)), this, SLOT(pageLoadFinished(bool)));..。
page->load(QUrl("http://google.com/"));...loading时间..。
void MaClasse :: pageLoadFinished(bool s){
page->toHtml([this](const QString &result){
qDebug()<<"html:";
qDebug()<<result;
item->setHtml(result);});
}https://stackoverflow.com/questions/36680604
复制相似问题