QWebEnginePage:toHtml返回一个空字符串 [英] QWebEnginePage: toHtml returns an empty string

查看:4637
本文介绍了QWebEnginePage:toHtml返回一个空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从 QWebEnginePage 中检索一些html。我在文档中找到了 toHtml 的方法,但它总是返回一个空字符串。我尝试 toPlainText
它可以工作,但这不是我需要。

I need to retrieve some html from a QWebEnginePage. I found in the documentation the method toHtml but it always returns an empty string. I tried toPlainText and it works, but this is not what I need.

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
}

我做错了什么?

推荐答案

我在QWebEngine。这是非常酷。我有以下工作。

I am getting my head around QWebEngine. It is very cool. I have got the following to work.

lambada捕获需要是所有的=,或者在发出信号的情况下为this。您还需要mutable来修改捕获的副本。 toHtml()是异步的,所以即使你捕获html,它不太​​可能在调用 toHtml()后直接可用 SomeFunction 。你可以通过使用信号和插槽克服这一点。

The lambada capture needs to be all that is "=", or "this" in the case of signal being emitted. You would also need "mutable" to modify the captured copies. toHtml() is however asynchronous so even if you capture the html it is unlikely it would be available directly after the call to toHtml() in SomeFunction. You can overcome this by using a signal and slot.

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;
}

这篇关于QWebEnginePage:toHtml返回一个空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆