How to prevent QWebEngineView to grab focus on setHtml(...) and load(...) calls? - qt

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;
For Qt 5.8 and newer
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:
QWebEngineView::settings()
QWebEngineSettings Class
QWebEngineSettings::setAttribute(WebAttribute attribute, bool on)
QTBUG-52999
For Qt 5.7 and earlier
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:
Initially it disables webView (webView->setEnabled(false)), so prevents to take focus on load(...), setHtml(...) calls.
When FocusWidget gets focus, it enables and forwards focus to webView, so enables keyboard input.
When webView and its children loses focus, FocusWidget disables webView again
The source code and sample application: https://github.com/rmisev/FocusWidget

I encountered the same problem in my app in PyQt5. What I managed to do is to disable the whole widget like this (in Python):
view = PyQt5.QtWebEngineWidgets.QWebEngineView()
view.setEnabled(False)
This resulted in view being operational with mouse (it accepts mouse clicks and things can be changed on web page) but i won't take focus from different widget.
I would definitely call it a bug instead of feature in QtWebEngineWidgets.

Thanks to all the others who provided answers for this. After encountering this bug I was somewhat stumped, but after learning of the way setEnabled(false) affects the focus stealing from the other answers I discovered that simply disabling the webview, setting the html or reloading it, then re-enabling it circumvents the issue (at least in Qt 5.7):
I.E.
myWebView->setEnabled( false );
myWebView->setHtml( html );
myWebView->setEnabled( true );
or
myWebView->setEnabled( false );
myWebView->reload();
myWebView->setEnabled( true );
This allows you to fix the problem without needing to subclass anything as one of the other answers suggests.

Related

Daydream keyboard implementation

I am attempting to implement the Daydream keyboard into an app built in Unity and am not able to get this to work. I have added the keyboard prefab as a sibling of the main camera and added two input fields with the onpointerclick function added as instructed. I however get a null reference exception and assume this is due to the daydream keyboard delegate field being blank. The example scene in the SDK shows the daydream delegate example prefab but I am unsure how to implement this for two input fields. Also does the keyboard render in the Unity editor or must it be built and run on a phone?
This is an old question and has probably already been answered, but I figured I'd publicize my answer anyway.
For those reading, if you haven't checked out the Keyboard Demo scene that can be found within the Demos folder of the Google VR Unity package, I would highly recommend doing so. Following this object hierarchy has worked for me in the past.
To answer your first question, it seems that they have included a KeyboardDelegateExample object within the scene's hierarchy, and then used this object as the Keyboard Delegate in the GVRKeyboardManager.
They manage to fake an Input Field by creating a background and overlaying a Text object on top. If this method does not suffice and using an Input Field is crucial in your particular case, then drop your Input Fields into two separate GVRKeyboardCanvas objects.
Clicking on either canvas will activate the GVR Keyboard. You may have to add a small script to manage the transitioning of the input field.
Lastly, no the GVR Keyboard does not render in the Unity Editor, it only appears while running a build. Hopefully this will be addressed in later releases. There are also Keyboard plugins that you may find useful on the Asset Store.

PyQt5 QMenu no focus on exec_()

I have an already fully functioning app, with a custom widget that opens a custom QMenu when clicked, by calling exec_():
menu.exec_(QPoint)
I recently migrated from PyQt4->PyQt5 and noticed that the focusOutEvent method I was overriding was no longer responsive. I already have a fix, which works just fine:
menu.setFocus() #calling this prior to exec_()
Nevertheless, I would like to understand what exactly is different after the Qt migration, that I had to manually set the focus in order to keep desired functionality.
The menu is set to have StrongFocus.
Another apparent issue that might be related is that the setting of the cursor to hand fails to change back to regular cursor when the cursor leaves the menu rect.
Thank you! =)

Qt Installer Framework : Hide the Back button

How to Hide the Back Button in Qt installer framework?
Please see attached image.
See my answer to Qt installer framework hide or disable buttons quoted below:
For the wizard BackButton specifically, it automatically disables itself if there are no pages before the current page a la the Introduction page.
From QtScript this can be accomplished by removing any dynamic pages before the current page with installer.removeWizardPage and disabling all default pages before the current page with installer.setDefaultPageVisible(QInstaller.Introduction, false).
There is void QWizard::setButton ( WizardButton which, QAbstractButton * button ) what means you schould be able to set a button which behaves like you need it. Derive a Class from QAbstractButton. Reimplement the paintEvent() to paint nothing and reimplement the mouseEvents to do nothing. That should do the (dirty) trick. Even if the wizard sets it to be visible, it won't draw itself and can't digest and mouse actions. Just tested it ... should work for you.

Disable UIPageViewController when ModalViewController opened

I am totaly new to this site, but I already like it :-)
I found it by searching for a question about the UIPageViewController.
I have a normal UIPageViewController App, in which I open a ModalViewController for setting up some settings...
Now the Problem: :-)
If I click on the done Button on the right side of the ModalView, to dismiss it, the PageViewController turnes the page, because he thinks that he is meant by that click ;-)
Can I disable the PageViewController GestureRecognizer as long as I have a ModalView opened?
Is there a method to disable and later his recognizer?
thank you for your help in advance...
cu Matze
It seems odd that your UIPageViewController would steal touches from a modal view presented over it. Unless, perhaps, you are embedding the modal view within the content of the UIPageViewController?
To answer your question -- you can easily disable the page view controller's gesture recognizers by enumerating its gestureRecognizers property (an NSArray):
for (UIGestureRecognizer *gr in [self.pageViewController gestureRecognizers]) {
[gr setEnabled:NO];
}
Re-enable them later with setEnabled:YES.
UPDATE:
In iOS 6 UIPageViewControllerTransitionStyleScroll has been added. UIPageViewControllers that use this transition style return no gesture recognisers in the array returned by gestureRecognizers. Presumably page view controllers with this transition style use an underlying, private UIScrollView instance (it behaves just like a UIScrollView with paging enabled), although I haven't checked this.

Qt can I drag and drop from widget to Qwebkit?

I'm developing simple HTML editor and I like to be able to drag and drop from a button that for example represent HTML text line and in the Qwebkit the HTML text line will be created does Qt support such action?
How should I approach such thing?
I believe it does, yes.
What you need to do is set the mime type of your drag event. See here for details. Then on the webkit side, you can read the drops mime type to see what it was.
You can then try one of the following approaches:
Subclassing QWebView to implement dragEnterEvent and dropEvent. You can use event->pos() in the dropEvent to get the position where the drop occured.
Implementing the drop in javascript within your page, eg setting up an event listener for drops or however its done (I've never tried this).

Resources