Qt WebEngine 模拟鼠标事件
Qt WebEngine simulate Mouse Event
我想在我的 Qt WebEngine 应用程序中模拟鼠标事件。
我用PyQt5.8,QT5.8.
这是我的代码:
def mouse_click(self, x, y):
point = QPoint(int(x),
int(y))
eventp = QMouseEvent(QMouseEvent.MouseButtonPress,point,Qt.LeftButton,Qt.LeftButton,Qt.NoModifier)
self.sendEvent(eventp)
eventp = QMouseEvent(QMouseEvent.MouseButtonRelease,point,Qt.LeftButton,Qt.LeftButton,Qt.NoModifier)
self.sendEvent(eventp)
def sendEvent(self, event):
recipient = self.webpage.view().focusProxy()
recipient.grabKeyboard()
self.application.sendEvent(recipient, event)
recipient.releaseKeyboard()
我测试了,但是没有用。我可以确认元素上的鼠标光标,但没有发生鼠标单击事件。谁能帮帮我?
我用的是Mac OS 10.12.4,我用另一个demo测试了一下,发现鼠标事件捕捉不到,但是其他事件可以捕捉。有什么建议么?
对于 Qt 5.8 运行 以下代码:
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);
我想在我的 Qt WebEngine 应用程序中模拟鼠标事件。
我用PyQt5.8,QT5.8.
这是我的代码:
def mouse_click(self, x, y):
point = QPoint(int(x),
int(y))
eventp = QMouseEvent(QMouseEvent.MouseButtonPress,point,Qt.LeftButton,Qt.LeftButton,Qt.NoModifier)
self.sendEvent(eventp)
eventp = QMouseEvent(QMouseEvent.MouseButtonRelease,point,Qt.LeftButton,Qt.LeftButton,Qt.NoModifier)
self.sendEvent(eventp)
def sendEvent(self, event):
recipient = self.webpage.view().focusProxy()
recipient.grabKeyboard()
self.application.sendEvent(recipient, event)
recipient.releaseKeyboard()
我测试了,但是没有用。我可以确认元素上的鼠标光标,但没有发生鼠标单击事件。谁能帮帮我?
我用的是Mac OS 10.12.4,我用另一个demo测试了一下,发现鼠标事件捕捉不到,但是其他事件可以捕捉。有什么建议么?
对于 Qt 5.8 运行 以下代码:
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);