Fixing QWebView - Prevent Drag & Drop and Allow New Windows

Writing hybrid apps based on QWebView, there are a few pitfalls you'll need to know about beyond documentation. Both preventing drag & drop and new windows had defaults that surprised me when I first encountered them, because they are different from how normal browsers act.

Drag & Drop

The default behavior of QWebView in regards to drag and dorp is not suitable for hybrid applications.
When a user drops external content onto the web view, the current page changes and the web view loads the dropped content.
The way to prevent users dropping content onto QWebView is to extend QWebView with your own, and override dropEvent.
An empty implementation will do the trick.

Another issue happens when users drag content out of your web view. If the QWebView displays local files, dragging content can lead to the files being moved by the operating system to another location (for example, if a user drags an image from an application displayed in a QWebView and drops it on her desktop, the image file will be moved to the desktop). This is usually the wrong behavior when writing hybrid apps.
Here, the workaround is simple - in your html add ondragstart handler to the body element, for example:

<body ondragstart="return false">
  ...
</body>

Opening New Windows

By default, QWebView does not open new windows, so if viewing an html that has:

<a href="..." target="_blank">Click</a>

Nothing will happen when the user clicks on the link. Again, this is easy to solve by implementing a custom QWebView and overriding createWindow method. The method should return a new QWebView in which to display the content of the link.

FlexiWebView

FlexiWebView is my small utility class which extends QWebView and provides methods to toggle both described behaviors. This basically saves you the trouble of extending QWebView and writing the short implementations for both methods. It's free to use, BSD-licensed and available on GitHub at:
https://github.com/ynonp/FlWebView

Clone and enjoy