1

I am making a WebAutomation tool,I would like to know how I could send mouse events to the WebEngineView. Here are some things I have tried but haven't work.

event= createMouseEvent(QEvent::MouseButtonPress, QPoint(mouse_x,mouse_y), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QApplication::instance()->sendEvent(view,event);

and

QTestEventList eventos;
eventos.addMouseClick(Qt::LeftButton, 0, QPoint(mouse_x,mouse_y), -1);
eventos.simulate(view);

where view is a QWebEngineView. I know that one can use javascript methods for clicking .But i would like to provide the user with a method of using mouse coordinates instead.

I would prefer a solution that is cross platform (I am also working on a linux version of the program) and can work when the window is minimised or not in view.

A noob friendly explanation will be greatly apprecieated. Please help. Thanks in advance.

Bob Kimani
  • 1,114
  • 1
  • 20
  • 38
  • 1
    QWebEngineView is a light QWidget wrapper around Chromium. Since page elements are not QWidgets, I would assume a QEvent wouldn't be able to interact with the page in any way. So you'd either have to send native mouse events -- which won't be cross platform -- or somehow expose Chromium's mouse handler so you could interact with that. – MrEricSir Aug 14 '16 at 23:41
  • Any news on this? I'm hitting pretty much the same wall now, but using Python3/PyQt5... – nostradamus Nov 18 '16 at 08:27
  • None..so far.. I just pray the devs add support for this on the next release. – Bob Kimani Nov 19 '16 at 08:36

1 Answers1

1

It seems that you have to access a child object of the QWebEngineView and then send events to it.The following is an implementation of it.

void LeftMouseClick(QWidget* eventsReciverWidget, QPoint clickPos)
{
    QMouseEvent *press = new QMouseEvent(QEvent::MouseButtonPress,
                                            clickPos,
                                            Qt::LeftButton,
                                            Qt::MouseButton::NoButton,
                                            Qt::NoModifier);
    QCoreApplication::postEvent(eventsReciverWidget, press);
    // Some delay
    QTimer::singleShot(300, [clickPos, eventsReciverWidget]() {
        QMouseEvent *release = new QMouseEvent(QEvent::MouseButtonRelease,
                                                clickPos,
                                                Qt::LeftButton,
                                                Qt::MouseButton::NoButton,
                                                Qt::NoModifier);
        QCoreApplication::postEvent(eventsReciverWidget, release);
    }));
}
QWebEngineView webView = new QWebEngineView();
// You need to find the first child widget of QWebEngineView. It can accept user input events.
QWidget* eventsReciverWidget = nullptr;
foreach(QObject* obj, webView->children())
{
    QWidget* wgt = qobject_cast<QWidget*>(obj);
    if (wgt)
    {
        eventsReciverWidget = wgt;
        break;
    }
}
QPoint clickPos(100, 100);
LeftMouseClick(eventsReciverWidget, clickPos);

Borrowing from the following answers Qt WebEngine simulate Mouse Event How to send artificial QKeyEvent to QWebEngineView?

Bob Kimani
  • 1,114
  • 1
  • 20
  • 38