0

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 or I should not use QTest for this?

user7179690
  • 1,051
  • 3
  • 17
  • 40
  • Make sure you use correct coordinates. Note, that `QCursor::pos()` returns position in screen coordinates. `QTest::mouseClick()` requires position in widget's coordinate system. – vahancho Jul 31 '17 at 11:55
  • Yes I use correct coordinates and I know that QCursor::pos() returns the position in screen so I use ui->Widget->mapFromGlobal to give me the position in the widget itself and I set parent of the QTestmouseClick to the widget and I am sure about the coordinates of the button in the website inside the QWebEngineView widget – user7179690 Jul 31 '17 at 12:06
  • what is the reason that you want to add QPushbutton inside the website? why don't you add a clickable item inside your html, use js code to get the x,y position and call to your Qt project via some proxy object ? – Simon Aug 01 '17 at 05:53

1 Answers1

0

I found the answer on QT forum which is
that 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;
    }
}

QTest::mouseClick(childThatAcceptsInput, Qt::LeftButton, Qt::KeyboardModifier::NoModifier, p);
user7179690
  • 1,051
  • 3
  • 17
  • 40