Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Unable send QKeyEvent to QWebEngineView
Servers for Qt installer are currently down

Unable send QKeyEvent to QWebEngineView

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 453 Views 1 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
    kingsta
    wrote on last edited by
    #1

    Hello friends

    I want to send some keys to displayed web page. I declared QWebEngineView and loaded "https://www.google.com/". There is no problem. But I can't send keys. Where is my problem? I couldn't find...

    header file

    class MainWindow : public QMainWindow
    {
    ...
    private:
    QWebEngineView *view;
    ...
    }
    

    my cpp files

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        view = new QWebEngineView(parent);
        QUrl url("https://www.google.com/"); 
        view->load(url);
    
    ui->horizontalLayout->addWidget(view);
    
        view->setEnabled(true);
        view->setFocusPolicy(Qt::TabFocus);
        ui->horizontalLayoutWidget->setFocus();
    
        connect(view, SIGNAL(loadFinished(bool)), this, SLOT(sendKeysFunction()));
    }
    ....
    
    void MainWindow::sendKeysFunction()
    {
    QKeyEvent *tabPress= new QKeyEvent ( QEvent::KeyPress, Qt::Key_Tab, Qt::NoModifier);
        QKeyEvent *tabRelease = new QKeyEvent ( QEvent::KeyRelease, Qt::Key_Tab, Qt::NoModifier);
    
        QCoreApplication::sendEvent (view->page()->view(), tabPress); 
        QCoreApplication::sendEvent (view->page()->view(), tabRelease );
        //Sleep(1000);
        QCoreApplication::sendEvent (view->page()->view(), tabPress);
        QCoreApplication::sendEvent (view->page()->view(), tabRelease );
         //Sleep(1000);
    
    
        QKeyEvent *enterPress = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier);
        QKeyEvent *enterRelease = new QKeyEvent ( QEvent::KeyRelease, Qt::Key_Return, Qt::NoModifier);
        QCoreApplication::sendEvent (view->page()->view(), enterPress );
        QCoreApplication::sendEvent (view->page()->view(), enterRelease );
    }   
    
    
    JonBJ 1 Reply Last reply
    0
    • K kingsta

      Hello friends

      I want to send some keys to displayed web page. I declared QWebEngineView and loaded "https://www.google.com/". There is no problem. But I can't send keys. Where is my problem? I couldn't find...

      header file

      class MainWindow : public QMainWindow
      {
      ...
      private:
      QWebEngineView *view;
      ...
      }
      

      my cpp files

      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          view = new QWebEngineView(parent);
          QUrl url("https://www.google.com/"); 
          view->load(url);
      
      ui->horizontalLayout->addWidget(view);
      
          view->setEnabled(true);
          view->setFocusPolicy(Qt::TabFocus);
          ui->horizontalLayoutWidget->setFocus();
      
          connect(view, SIGNAL(loadFinished(bool)), this, SLOT(sendKeysFunction()));
      }
      ....
      
      void MainWindow::sendKeysFunction()
      {
      QKeyEvent *tabPress= new QKeyEvent ( QEvent::KeyPress, Qt::Key_Tab, Qt::NoModifier);
          QKeyEvent *tabRelease = new QKeyEvent ( QEvent::KeyRelease, Qt::Key_Tab, Qt::NoModifier);
      
          QCoreApplication::sendEvent (view->page()->view(), tabPress); 
          QCoreApplication::sendEvent (view->page()->view(), tabRelease );
          //Sleep(1000);
          QCoreApplication::sendEvent (view->page()->view(), tabPress);
          QCoreApplication::sendEvent (view->page()->view(), tabRelease );
           //Sleep(1000);
      
      
          QKeyEvent *enterPress = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier);
          QKeyEvent *enterRelease = new QKeyEvent ( QEvent::KeyRelease, Qt::Key_Return, Qt::NoModifier);
          QCoreApplication::sendEvent (view->page()->view(), enterPress );
          QCoreApplication::sendEvent (view->page()->view(), enterRelease );
      }   
      
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @kingsta
      I don't know, but have a read through:
      https://stackoverflow.com/questions/27894769/how-to-send-artificial-qkeyevent-to-qwebengineview
      https://stackoverflow.com/questions/40960388/simulate-mouse-click-for-qwebengineview

      It seems peeps there are sending to QWebEngineView::children().

      It seems that you have to access a child object of the QWebEngineView and then send events to it.

      ?

      1 Reply Last reply
      0
      • eyllanescE Offline
        eyllanescE Offline
        eyllanesc
        wrote on last edited by
        #3

        @kingsta Use view->focusProxy() as discussed in this post https://stackoverflow.com/questions/57011407/qt-event-propagation-in-qwebengineview:

        void MainWindow::sendKeysFunction()
        {
            QKeyEvent *tabPress= new QKeyEvent ( QEvent::KeyPress, Qt::Key_Tab, Qt::NoModifier);
            QKeyEvent *tabRelease = new QKeyEvent ( QEvent::KeyRelease, Qt::Key_Tab, Qt::NoModifier);
        
            QCoreApplication::sendEvent (view->focusProxy(), tabPress);
            QCoreApplication::sendEvent (view->focusProxy(), tabRelease );
            //Sleep(1000);
            QCoreApplication::sendEvent (view->focusProxy(), tabPress);
            QCoreApplication::sendEvent (view->focusProxy(), tabRelease );
            //Sleep(1000);
        
            QKeyEvent *enterPress = new QKeyEvent ( QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier);
            QKeyEvent *enterRelease = new QKeyEvent ( QEvent::KeyRelease, Qt::Key_Return, Qt::NoModifier);
            QCoreApplication::sendEvent(view->focusProxy(), enterPress );
            QCoreApplication::sendEvent(view->focusProxy(), enterRelease );
        }
        

        If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kingsta
          wrote on last edited by
          #4

          @eyllanesc and @JonB

          You think are right! I can send events now. Thank you a lot.
          I read some topics before that you send, but I couldn't do that.

          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