c++ - get HTML from QWebEnginePage in QWebEngineView using Lamda -


i want html code of web page opened in qwebengineview use tohtml() function in qwebenginepage class this

qwebenginepage *page = ui->widget->page(); qstring html = ""; page->tohtml([&html](qstring html){qdebug() << "code \n\n\n" << html;}); 

the html code of html page appeared in qdebug without problem problem here when want use html string outside function when show size of html varible equal 0 , empty tried this

qwebenginepage *page = ui->widget->page(); qstring html = ""; page->tohtml([&html](qstring html){html = html;}); // crash qdebug() << "i want use html here outside function = " << html; 

but app crash show should put html data in html variable can use outside function
in advance

your problem caused fact lambda run asynchronously. called after have exited method in call tohtml method , explains crash - html local variable within method has exited lambda randomly corrupts memory used occupied html variable.

what want here synchronize things i.e. block method until lambda executed. can done qeventloop need involve sending special signal lambda indicate fact lambda finished executing. (non-tested):

class myclass: public qobject {     q_object public:     myclass(qwebenginepage & page, qobject * parent = 0);      void notifyhtmlreceived();      qstring gethtml();     void sethtml(const qstring & html) { m_html = html; }  q_signals:     void htmlreceived();  private q_slots:     void requesthtmlfrompage();  private:     qwebenginepage & m_page;     qstring m_html; };  myclass::myclass(qwebenginepage & page, qobject * parent) :     qobject(parent),     m_page(page) {}  void myclass::notifyhtmlreceived() {     emit htmlreceived(); }  qstring myclass::gethtml() {     qeventloop loop;     qobject::connect(this, signal(htmlreceived()), &loop, slot(quit()));      // schedule slot run in 0 seconds not right     qtimer::singleshot(0, this, slot(requesthtmlfrompage()));      // event loop block until lambda receiving html executed     loop.exec();      // if got here, html has been received , result saved in m_html     return m_html;    }  void myclass::requesthtmlfrompage() {     m_page.tohtml([this](qstring html)                   {                       this->sethtml(html);                       this->notifyhtmlreceived();                   }); } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -