Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Unable send QKeyEvent to QWebEngineView



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


  • @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.

    ?



  • @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 );
    }
    


  • @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.


Log in to reply