How to lose selection focus of a QTextEdit? - qt

I have a readonly QTextEdit. I'm able to select the text and copy it.
What I need to do: When the user is in selection mode(selecting text), if he leaves the area (with the left mouse button still holding) the widget must lose focus and selection focus must end too. When mouse exits out of bounds I want to be similar to mouse release. I tried to set this:
rootWidget->clearFocus();
rootWidget->close();
rootWidget->releaseMouse();
but even though visually the widget seems out of focus I still receive the TextEdit::mouseMoveEvent() of my widget.
I also tried to set focus to a dummy widget. The dummy widget gets focused but my textedit sitll gets mouseMoveEvent().
How can I lose focus of the widget completely without releasing the mouse button?
Thank you for any suggestion!

You can give the focus to something else using QWidget::setFocus(). you should probably clear the selection before changing focus.

Related

Is there a way to show tooltip on disabled QWidget

I have a Qt form, where I have a button and menu. For various reasons I can disable certain elements, e.g button or some actions in the menu.
Is there a way I could show a tooltip or when the mouse is hovered over the disabled button or menu item with an explanation as to why it is disabled?
I am using Qt 4.8.
Thanks!
You can set the tooltip dynamically based on the state of the QWidget or by simply toggling both at the same time. Upon disabling/enabling the widget from somewhere just call QWidget::setToolTip(...) with the QString you want the tooltip to display when hovering with the mouse over the given widget. For example if you have a public slot called toggleButton(bool toggleFlag) which toggles the enable-setting of a button you can do:
void MyWidget::toggleButton(bool toggleFlag) {
this->ui->myButton->setEnabled(toggleFlag);
this->ui->myButton->setToolTip(toggleFlag ? QString("Enabled wohoo!") : QString("Disabled because I like it"));
}
You can of course do also change the tooltip by calling QWidget::isEnabled() and act upon its return value. Since you haven't given any code I can only assume how you toggle your button(s) so that's all I can give you for now.
UPDATE: It was pointed in the comments that tooltips don't work with disabled widgets due not receiving mouse events. Both statements are not true (note that I have used the same tooltip message since due to lack of minimal working example I didn't want to write a whole new project from scratch and used an existing one of mine instead):
Hovering a disabled button triggers the tooltip
Hovering an enabled button triggers the tooltip

How to check if the mouse has really left the QWidget?

Consider a QWidget window, what event is triggered when the mouse has left this window?
The window has QLineEdit fields on it and they have completers (QCompleter) for input suggestions. The actual goal is to make such an (open) completer disappear when the mouse leaves the window. This is mostly because in some environments, moving the mouse over a different window may focus that other window, but keyboard events are still sent to the QLineEdit field (even though its parent window isn't focused anymore), which is confusing.
I could implement QWidget::leaveEvent(QEvent *event) (in the window), find the currently shown completer popup and hide it, that closes the popup. But ironically, leaveEvent() is also triggered when the mouse is moved over that popup - hiding it (making it impossible to click on an item in that popup). I guess that makes sense because the popup is a different QWidget, even though the popup is indirectly owned by the window.
So how do I check if the mouse has actually left the QWidget window?
Reimplement QWidget::leaveEvent(QEvent *event) in your derived class but start by checking that rect().contains(mapFromGlobal(QCursor::pos())) is true.
If not, return without doing anything.
This should filter out all the events where the mouse is still over your widget.
Hope it helps!

Keep Selection in JavaFX Textfield even if application is not focussed

Background:
I'm trying to implement a find/replace function for java-Fx table view. Whenever I find an occurance of the text to find, I switch the table view into edit mode and select the text found programatically in the textfield which is used during edit mode.
The focus in this situation remains in the modal find/replace dialog, so the selection in the textfield is not visible until I close the modal dialog.
The problem:
A textfield in Java-Fx shows it's selection only while it has the focus (as its standard behaviour). When the Java-Application loses focus the selection becomes invisible, when it gets the focus back the selection is visible again.
Here is the question:
Is it possible to keep the selection visible in a textfield though focus is lost?
What I tried:
I expected that CSS could help here:
textField.setStyle("-fx-highlight-fill: lightgray");
This changes the color of the highlighting but
I wanted to keep the original color for the focussed Textfield
I wanted to change the color from"invisible" to "lightgray" only for
the non-focussed Textfield
What is the correct way?
Or is there a way to highlight text in a TableView without activating the TextField?
Thanks
Ingo
If I am understanding correctly, the solution will be in the modality of your find/replace dialog.
It works for me when I do the following: Note that the java file this is written in extends Stage. If you are not extending stage then just call the methods on your find/replace stage.
owner = myApplicationStage;
initModality(Modality.NONE);//important for the solution!
initOwner(owner);
initStyle(StageStyle.UTILITY);
setScene(myFindAndReplaceScene);
stage = this;
stage.show();
This works perfectly for me, if you have any problems then hopefully I can help.

How to detect a mouse click inside a tooltip in Qt?

I have a Qt application in which certain QLabel's display a tooltip if a certain internal condition occurs.
Since the tooltip has a timeout and is hidden by Qt automatically, the tooltip is kept alive by the application by showing the tooltip every 3 seconds (I did not find any mechanism to tell Qt to show a tooltip indefinitely).
The tooltip is displayed until the user clicks on the QLabel itself: the tooltip is not refreshed any more and disappears.
I now have a new requirement that the tooltip should also disappear if the user clicks on the tooltip itself. Is there a signal that is sent when the user clicks on a visible tooltip? Or is it necessary to use some more sophisticated technique?
EDIT
I have checked my code again, here is some extra information.
Qt does detect a mouse click on a tooltip and hides it, but the application immediately shows the tooltip again.
What I would like to do is that Qt informs my code of the mouse-clicked event, so that it stops showing the tooltip again and again. I have two possible solutions in mind, but I do not know if either of them is technically feasible:
Remove tooltip timeout: when a tooltip is shown, it remains visible as long as the user does not click on it. How can I display a tooltip without a timeout in Qt?
Keep the tooltip visible by repeatedly showing it; detect a mouse clicked on the tooltip in order to stop the keep-alive loop. How do I receive a signal when the user clicks on a tooltip?
Under the hood, the actual class that implements the tooltip is a private QLabel-derived class called QTipLabel:
http://qt.gitorious.org/qt/qt/blobs/4.7/src/gui/kernel/qtooltip.cpp#line119
There's nothing published in the interface to get at an instance of that class from QToolTip, so intercepting clicks would only be done with some sort of ill-advised hack.
That said: among the events that should "hideTipImmediately" is QEvent::mouseButtonPress...in fact, there's an event filter installed so that any click in the app will hide it:
http://qt.gitorious.org/qt/qt/blobs/4.7/src/gui/kernel/qtooltip.cpp#line325
So if you're not seeing the tooltip disappear when it's being clicked on, there's a bug. (Clicking on tooltips hides them for me in Qt-based apps under Kubuntu.)
Note the definition of QToolTip::showText:
void QToolTip::showText ( const QPoint & pos, const QString & text,
QWidget * w, const QRect & rect ) [static] Shows text as a tool tip,
with the global position pos as the point of interest. The tool tip
will be shown with a platform specific offset from this point of
interest.
If you specify a non-empty rect the tip will be hidden as soon as you
move your cursor out of this area.
The rect is in the coordinates of the widget you specify with w. If
the rect is not empty you must specify a widget. Otherwise this
argument can be 0 but it is used to determine the appropriate screen
on multi-head systems.
If text is empty the tool tip is hidden. If the text is the same as
the currently shown tooltip, the tip will not move. You can force
moving by first hiding the tip with an empty text, and then showing
the new tip at the new position.
Thus, you can supply the rectangle in which the QToolTip is to be presented. Then, if what you want to do is close the QToolTip only when the user clicks on it, you can capture mouseButtonPress events as #HostileFork pointed out and then close the tooltip only when the coordinates of the event fall within it.

How to force calling of QWidget::paintEvent() when its hovered by other window?

I have a problem:
I'm creating a widget which displays current date's day number. It's like a button, but it's not derived from QPushButton class. Just from QWidget. So I've reimplemented enterEvent(), leaveEvent(), mousePressEvent(), mouseReleaseEvent(). I do call update() inside these methods and widget has realistic button behavior (paintEvent() is reimplemented also).
But when I change system date and hover that widget with other window, my widget doesn't get paintEvent() and the old date is displayed. Only when I place mouse over it, widget repaints it's contents.
I guess there is a feature (like buffering) which paints old contents on hovering with other window to avoid unnecessary recalculations. But I need to disable it. Tried to set many attributes (the Qt::WidgetAttribute enum). But it doesn't work.
I think you should find a way to detect that the system time has changed and call update() when that happens. Any other method (like detecting the "hovering" of a window or waiting for a mouse event) will cause the update to occur too late.

Resources