I have an editable QWebView
. In eventFilter
, I want to change the Qt::Left_Key
event to Qt::Right_Key
and vise versa for textCursor position in webview. Here's my code:
bool MyClass::eventFilter(QObject *o, QEvent *e)
{
if(o == p->webView) {
switch(static_cast<int>(e->type()))
{
...
case QEvent::KeyPress:
if(static_cast<QKeyEvent*>(e)->key() == Qt::Key_Left) {
QKeyEvent* event = new QKeyEvent(QEvent::KeyPress, Qt::Key_Right, Qt::NoModifier);
QApplication::postEvent(p->webView, event);
return true;
}
else
if(static_cast<QKeyEvent*>(e)->key() == Qt::Key_Right) {
QKeyEvent* event = new QKeyEvent(QEvent::KeyPress, Qt::Key_Left, Qt::NoModifier);
QApplication::postEvent(p->webView, event);
return true;
}
break;
}
}
return QWidget::eventFilter(o,e);
}
But when I create a QKeyEvent and post it to application, I guess the eventFilter call again for QKeyEvent that I posted to application and webview textCursor that moved to left (for example), again move to right and seems it's position don't change.
How can I solve this problem? Can anyone help?