Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QtWebEngine
  4. QWebEngineCallback? &resultCallback? How does it work?
Forum Updated to NodeBB v4.3 + New Features

QWebEngineCallback? &resultCallback? How does it work?

Scheduled Pinned Locked Moved Solved QtWebEngine
callbackqwebenginetohtml
3 Posts 2 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    desun_evan
    wrote on 13 Nov 2022, 06:48 last edited by desun_evan
    #1

    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?

    J 1 Reply Last reply 13 Nov 2022, 08:34
    0
    • D desun_evan
      13 Nov 2022, 06:48

      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?

      J Offline
      J Offline
      JonB
      wrote on 13 Nov 2022, 08:34 last edited by JonB
      #2

      @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 use QString 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 the 1, 2, 3.

      D 1 Reply Last reply 13 Nov 2022, 08:57
      2
      • J JonB
        13 Nov 2022, 08:34

        @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 use QString 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 the 1, 2, 3.

        D Offline
        D Offline
        desun_evan
        wrote on 13 Nov 2022, 08:57 last edited by desun_evan
        #3

        @JonB Thank you so much! I see this is different than I thought.

        Works good!

        1 Reply Last reply
        0
        • D desun_evan has marked this topic as solved on 19 Oct 2023, 01:06

        1/3

        13 Nov 2022, 06:48

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved