I am currently working on developing a simple web browser for an embedded system using Qt. The browser incorporates its own keyboard.
However, I am facing difficulties when it comes to interacting with the web page using the QWebEngineView
class, using the generated events to do it.
I have identified two potential solutions for this problem:
- One option is to utilize
QWebEnginePage::runJavaScript
to interact with the page and call the keyboard. Although I have been able to achieve this using the mentioned method, I consider it to be an unreliable solution with numerous flaws. - Another approach involves using an
eventFilter
to capture input events, specifically targeting theQEvent::InputMethodQuery
event. Unfortunately, this solution appears to be unstable as the event is triggered multiple times when I click on an input area within the web page.
Below is a snippet of the code, where m_view
is a pointer to QWebEngineView
:
void WebBrowser::activate(void)
{
m_view->setUrl(QUrl(tr("https://www.google.com/")));
m_view->show();
// RenderWidgetHostViewQtDelegateWidget is created after loading a page
// so you must access it after load() or setHtml().
QWidget *render_widget = m_view->focusProxy();
render_widget->installEventFilter(this);
render_widget->setFocusPolicy(Qt::ClickFocus);
}
bool WebBrowser::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::InputMethodQuery)
{
auto inputEvent = static_cast<QInputMethodQueryEvent *>(event);
if (inputEvent->queries().testFlag(Qt::ImCursorRectangle))
{
obj->removeEventFilter(this);
callKeyboard();
obj->installEventFilter(this);
}
return true;
}
else
{
return QObject::eventFilter(obj, event);
}
}
I have already attempted to utilize QEvent::FocusIn
, QEvent::RequestSoftwareInputPanel
, and even QWidget::focusInEvent
, but none of these approaches produced the desired results.
Do you have any suggestions for improving this code? It is challenging to find comprehensive examples for more complex scenarios like handling input events.