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. QWebEngineView Best Way to Wait for Asynchronous Data

QWebEngineView Best Way to Wait for Asynchronous Data

Scheduled Pinned Locked Moved Solved QtWebEngine
3 Posts 2 Posters 2.8k Views 2 Watching
  • 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.
  • K Offline
    K Offline
    kmbar2013
    wrote on last edited by p3c0
    #1

    What is the best way to receive data if asynchronous? When the user clicks a QPushButton, I would like to query for data, however the runjavascript call takes too long to retrieve the data.

    I have tried the following:

    void MainWindow::getInformation()  // slot
    {
        MyData data;
        m_webEngineView->executeCommand("getData(\'id_name\')");
        // problem is here because when the following call is made
        //  the data has not been updated yet
        data = m_webEngineView->getData();
    }
    

    Snippet from WebEngineView

    void MyWebEngineView::executeCommand(QString cmd)
    {
        page()->runJavaScript(cmd, myFunction);
    }
    void MyWebEngineView::myFunction(QVariant myvar)
    {
        setData(myvar);
    }
    void MyWebEngineView::setData(QVariant myvar)
    {
        // manipulate myvar to fill in m_data
        m_data = myvar.toMyData();
    }
    MyData MyWebEngine::getData()
    {
        return m_data;
    }
    

    Thanks in advance for any help.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      the problem is that you want a synchronous method MainWindow::getInformation() using asynchronous API.

      My suggestion is to have a signal in your MyWebEngineView and emit it when the callback is executed

      // MyWebEngineView.h
      class MyWebEngineView:....
      {
      Q_SIGNALS:
          void dataReceived();
      };
      
      // MyWebEngineView.cpp
      void MyWebEngineView::setData(QVariant myvar)
      {
          m_data = myvar.toMyData();
          
          Q_EMIT dataReceived();
      }
      

      and then connect this signal to a slot in MainWindow

      connect(m_webEngineView, &MyWebEngineView::dataReceived, this, &MainWindow::handleData);
      
      void MainWindow::handleData()
      {
          Mydata data = m_webEngineView->getData();
      
          // Use data
      }
      

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      K 1 Reply Last reply
      1
      • M mcosta

        Hi and welcome to devnet,

        the problem is that you want a synchronous method MainWindow::getInformation() using asynchronous API.

        My suggestion is to have a signal in your MyWebEngineView and emit it when the callback is executed

        // MyWebEngineView.h
        class MyWebEngineView:....
        {
        Q_SIGNALS:
            void dataReceived();
        };
        
        // MyWebEngineView.cpp
        void MyWebEngineView::setData(QVariant myvar)
        {
            m_data = myvar.toMyData();
            
            Q_EMIT dataReceived();
        }
        

        and then connect this signal to a slot in MainWindow

        connect(m_webEngineView, &MyWebEngineView::dataReceived, this, &MainWindow::handleData);
        
        void MainWindow::handleData()
        {
            Mydata data = m_webEngineView->getData();
        
            // Use data
        }
        
        K Offline
        K Offline
        kmbar2013
        wrote on last edited by
        #3

        @mcosta Your wording of my question was better. Thanks for the reply! I guess I was being lazy and hoping to avoid having 2 functions to call one. :-D

        1 Reply Last reply
        0

        • Login

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