0

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 the QEvent::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.

Lerxt
  • 1
  • 1
  • Without reading the entire question, just generally about this: The Chromium engine runs the web page, and Qt framework has limited access to what happens there. So injecting JavaScript and overriding the web engine class virtual methods are your ways to do what you need to do, Qt events don't usually get generated or used. – hyde May 23 '23 at 17:27
  • I understand. My main question is when to call the virtual keyboard show function and send the data to the webpage. How does Qt determine when to display the virtual keyboard? There should be an event to show or hide the keyboard. The Qt examples in the folder didn't help much as they use QML instead of pure C++. I found **KWin** code using `QEvent::InputMethod` and `QEvent::InputMethodQuery`, but the first event never happens in my app, and the second event is called multiple times, making it hard to show the keyboard once. – Lerxt May 23 '23 at 17:40

0 Answers0