Query which QT element has focus - qt

Is it possible to query which QT element currently has focus?
I'm currently working with a program and the action I want to implement requires a specific element to have focus. Instead of trying to track down through the hierarchy from the top which element I need, I'm wondering if it's possible to select the element in the UI and then query it so that I can trace it's path upwards.

If you want to know that QWidget has the focus then use the QApplication::focusWidget() method, you can also use the focusChanged signal

Related

Programmatically Cycle through Tab-Order in QT5

I have a QT5 application in which I have to simulate a tab keypress on an external event i.e. I need to cycle through the taborder list and setFocus on the next element.
I cannot find any method to get the tab-order list programmatically. What would be the best way to do this?
How about a combination of QWidget:: nextInFocusChain() / previousInFocusChain() and setFocus() method or setFocus() slot? (There are some other ways to set focus as well, all cross-linked in the Qt docs.)
nextInFocusChain()and previousInFocusChain() provide public API access to the underlying private members which seem to control the tabbing order. You can see them being used in QWidget::setTabOrder() for example.
Current focus widget could be found with either QWidget::focusWidget() of the parent widget, or QApplication::focusWidget(). Note that QWidget::focusWidget() will also return the first widget in the focus order if none currently have focus. This starting point could be used to build a list of widgets in tab order.

How to replace all cursor appearances within a QML program?

I'm working on a game that uses QML for its UI.
I would like to replace all cursors appearances with cursor-images that are more fitting to the game style (e.g. pointing skeleton hand instead of the normal Qt::ArrowCursor).
Calling QGuiApplication::setOverrideCursor() seams not to be a practical solution as I can not "overwrite" each MouseArea to call may replaceCursor() magic-global-function. For example the change column with cursor within a TableView is currently impossible for me to manipulate.
To me the most practical solution would be to replace the appearance of all cursors but leaf Qt with the tasks to correctly choose the cursor style.
Thanks for any help!
You can still use QGuiApplication::setOverrideCursor() to decorate your mouse areas. It works like a stack, you can set and then restore cursors, so you begin with setting an initial cursor from main.cpp, and then you use an "overloaded" MouseArea which sets its cursor using setOverrideCursor() as well, instead of using the QML functionality.
For example:
onContainsMouseChanged: {
if (containsMouse) Sys.setOverrideCursor(yourCursortype)
else Sys.restoreOverrideCursor()
}
Of course, that means you will have to create an auxiliary object that will call those functions from C++, and expose it to QML so it can be called from there.

Codeception, WebDriver | How to hover, then click at a hovered location

I am using WebDriver and Asserts as modules in my acceptance testing.
Using WebDriver, I am trying to click on a label that acts as the javascript anchor for a form checkbox (the actual checkbox being hidden and a ::before font element being used to represent the checkbox as checked or not).
There is a link in this label which is located at the center of the element; the position I assume is targeted by the click() method. Due to this, I can't just click() on the element, as it will click the link instead of triggering the checkbox.
I envisioned that the solution for this problem would be to moveMouseOver(), using coordinates arguments in order to displace it to the side, then to trigger a click... but click() does not allow for a click event at the current cursor location, instead requiring a selector to be applied, thus defeating my purpose.
Is there any way to accomplish what I am attempting in the current WebDriver module in Codeception? Alternately, is there a way to accomplish this targeting of an uneven element for clicking without the process I've outlined?
Thank you for whatever help you are able to give.
I use a workaround: just making element visible via JavaScript:
$I->executeJS("$('css_selector').css({'display':'block'});");

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.

How to find the first focusable component in a container?

I'm trying to programatically set a focus on the first component of a given container. What's the best way to do it?
A couple of notes:
I haven't found any appropriate method on IFocusManager / IFocusManagerContainer interfaces. Ideally, there would be IFocusManagerContainer#getFirstFocusableComponent() but there is no such method.
The "first" component is the one that would receive focus if you were on some component just before the container and pressed TAB. It is not necessarily the first focusable child of the container (tab order plays role here).
I'd like the solution to perform well. I'm not particularly keen on traversing display list, I still hope there is some better way.
Thanks!

Resources