Qt can I drag and drop from widget to Qwebkit? - qt

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).

Related

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

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.

Editing the navigation controller from push segue

So i have a static tableview with 4 rows, i'm connecting them to different viewcontrollers without using any code instead i'm simply dragging the cell and setting up the segue.
The issue i'm having is that when this segue is performed a navigation controller is automatically generated and embed at the top so i get the following result below.
How could i edit the text and the icon? I want to remove the settings text and use my own custom icon.
It's also worth noting that i have embed other navigation controllers throughout my app. So i'd like to target this view specifically rather than all of my views.
I suspect the accepted answer on this question may help: how to replace/customize back button image in storyboard navigationcontroller
You'll need to do it in your prepareForSegue I believe.

How to draw something in a tooltip?

I am trying to develop a functionality using Qt which I don't know whether it is possible to implement. Here is the requirement:
When the user hovers over a node (an object derived from QGraphicsItem), a window will be shown near the node, in the window there might be some histograms or buttons that can be clicked to show further information. When the mouse leaves the window, it will automatically close.
I tried to use a tooltip, because it can pop-up near the node and close when the mouse leaves it, but it can only show text. So, it still cannot work that way. I am wondering if there is another way to do this? I did lots of google search, but there is still no answer.
Thanks so much for helping me with this.
If you're ok with using a 3rd party library, Qxt provides a class that provides a tooltip that is QWidget based, which will let you use an arbitrary widget as the tooltip rather than just text.
See: Qxt::ToolTip
you don't have to use tooltip for your app
you can use or call widget or dialog, on hover mouse event
Please refer Qt Example EmbeddedDialog Example, It is advanced, But you can understand how hover Enter/Leaving events are working. I personally prefer don not create instance of Popupdialog for each item, create it if only nesessary. Otherwise create one dialog and pass its reference to all the items through the constructor initialization.
1. These are the API you are intrested on, reimplemet this.
QGraphicsItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) and void QGraphicsItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
2. When You create Dialog, You can pass Qt::WindowFlags as Qt::ToolTip.

PyQt how to add link to QTextBrowser

I don't know why I'm having such a hard time finding this, but how do you add a hyperlink to QTextEdit or QTextBrowser? I'm trying to make something that has a list of links and when you click on a link you get the object that the link is referencing.
You need to set the acceptRichText property to true. Then you can just pass in HTML using the setHTML() slot.
win.setHTML("<a href="http://foo>Bar</a>");

How to make Qt widgets do not react on mouse click

I need on my form ordinary widgets (e.g. buttons) do not react on mouse clicks but NOT to be disabled (it change look to grayed one -- not good).
I wonder is there some neat small hack for this?
You could stick in an event filter and filter out the mouse events before passing the remaining events on for processing, but I'm not sure that not giving the user a visual clue that certain elements are effectively disabled is such a good idea.
You could try using style sheets to control how the disabled mode of the buttons in your form get styled. Unfortunately I'm not sure exactly how to do that but you could have a look at the style sheet docs to get you started.

Resources