How to make a button press on a point (x, y) in QWidget



  • I am using QWebEngineView to make a web view to my project so first I add a widget in the UI form and promoted it to QWebEngineView it works fine now I want to make a button press inside this widget on a point x,y (which is a button inside the website viewed in the widget)
    so I make a keypress when I press on S from the keyboard it save the point of the mouse hover inside this widget (I hover exactly on the button inside the website)
    I use this code to save the points

    bool MainWindow::eventFilter(QObject *watched, QEvent *event)
    {
        if (event->type() == QEvent::KeyPress)
        {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            if(keyEvent->key() == Qt::Key_S) {
                p = ui->widget->mapFromGlobal(QCursor::pos()); //QPoint varible I use it to save the x,y inside the widget
                qDebug() << "point inside the widget = " << p.x() << " " << p.y();
            }
        }
        return QObject::eventFilter(watched, event);
    }
    

    then I have button when i press it should make a button press inside the QWebEngineView widget I use this code inside my button

    QTest::mouseClick(ui->widget, Qt::LeftButton, Qt::KeyboardModifier::NoModifier, p);
    //p  saved the location of the button in the webview widget
    

    but it didn't press that button or make this button press ? so what is the problem of my code which make it not press on the button inside the webview



  • Hi, the QWebEngineView doesn't like mouse events, instead we have to find the first child widget inside it, here's some code to try before QTest::mouseClick():

    // find the kid inside QWebEngineView that will accept mouse events
    QWidget* childThatAcceptsInput = nullptr;
    for (auto w : ui->widget->children())
    {
        QWidget* child = qobject_cast<QWidget*>(w);
        if (child)
        {
            childThatAcceptsInput = child;
            break;
        }
    }
    
    // ok, mouseClick the kid
    QTest::mouseClick(childThatAcceptsInput, Qt::LeftButton, Qt::KeyboardModifier::NoModifier, p);


  • Thank you that works fine


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.