I have created simple Qt Window Layout with QTreeView
and QWebEngineView
: after selecting some item in the tree view, the Web engine view shows some content. The problem is what when QWebEngineView::setHtml(...)
or load(...)
is called the tree view loses keyboard focus and Web engine view gets it. This causes difficulties when selecting items with keyboard in the tree view. So, how to prevent the tree view focus lost?
I tried to use QTextBrowser
instead of QWebEngineView
. It doesn't have this problem, but it is not suitable for complex HTML pages.
Suppose we have:
QWebEngineView *webView = new QWebEngineView;
The problem can be solved by tweaking settings:
webView->settings()->setAttribute(QWebEngineSettings::FocusOnNavigationEnabled, false);
The sample code: https://github.com/rmisev/FocusWidget/tree/if-qt-5.8
References:
The simplest solution (also pointed by @Netrix) is to call:
webView->setEnabled(false);
But this disables keyboard input to the webView
.
To solve this problem I created the simple FocusWidget
class as parent widget for webView
, which works as follows:
webView
(webView->setEnabled(false)
), so prevents to take focus on load(...)
, setHtml(...)
calls.FocusWidget
gets focus, it enables and forwards focus to webView
, so enables keyboard input.webView
and its children loses focus, FocusWidget
disables webView
againThe source code and sample application: https://github.com/rmisev/FocusWidget