What Object has Active Focus in QML? - qt

i have a complex GUI with QML but in some situation i lost my focus and i don't know what object has active focus.
Is there any tools or ways to search in QML files and find focused Object?

I use this line to see which item has active focus:
Window {
onActiveFocusItemChanged: print("activeFocusItem", activeFocusItem)
}
This code responds to changes in the activeFocusItem property of Window by printing out the item with active focus. ApplicationWindow from Qt Quick Controls 1 and 2 have the same property since they derive from Window.
To find out how an item got focus, you can set the QT_LOGGING_RULES environment variable to qt.quick.focus = true. This enables logging for Qt's internal focus handling. The output can be a bit tricky to follow though..
Since you're using Qt Quick Controls 2, it's worth noting that each control has a focusPolicy property which determines how the control gets focus. The default for controls like Button is Qt.StrongFocus, which means that buttons get focus after being clicked or tabbed into. If you're seeing that a control has focus and you don't want it to, just set its focusPolicy to Qt.NoFocus:
focusPolicy: Qt.NoFocus

Related

qml how to keep focus on TextField

I want to keep focus on TextField. For example i am typing something then tap to button. The focus on TextField is moving to button, that's why keyboard is automatically hiding on Android. I am using Qt 5.9.2. Thanks in advance!
In Qt Quick Controls 2, each control has a focusPolicy property which determines how the control gets focus. The default for controls like Button is Qt.StrongFocus, which means that buttons get focus after being clicked or tabbed into. If you're seeing that a control has focus and you don't want it to, just set its focusPolicy to Qt.NoFocus:
focusPolicy: Qt.NoFocus

How to gray a group of widgets when they are disabled?

There is a QGroupBox full of widgets, and all of them need to be disabled and grayed out.
setDisabled(true) does disable them functionally, but they don't turn gray.
What is the easiest and most proper way to turn them gray?
This should be a standard operation: text turns gray so that users can easily see that they are disabled.
QWidget::setDisabled() is yet (another) slot for the QWidget enabled property.
From Qt doc.:
This property holds whether the widget is enabled
In general an enabled widget handles keyboard and mouse events; a disabled widget does not. An exception is made with QAbstractButton.
Some widgets display themselves differently when they are disabled. For example a button might draw its label grayed out.
(Emphasizing by me.)
How widgets are displayed depends on their respective rendering as well as the (default) QStyle which is used in the application.
Concerning custom widgets (classes derived from any "built-in" Qt widget) where paintEvent() is overloaded, the custom widget is itself responsible to render accordingly to different states.

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.

Qt: Custom QListView and live controls

My custom QListView has delegates to paint the items. I'd like to add a live control to some of the row items (like a QLineEdit), that'll always be present in the row and will automatically scroll correctly with the list.
Since items are not widgets, I cannot assign a control to be a child of an "item", thus scrolling will leave the control in its fixed spot within the QListView widget.
Is there another way?
Is that even possible?
Normally the edit widget is created (and positioned) by the delegate when an QEvent::EnterEditFocus event occurs then destroyed when a subsequent QEvent::LeaveEditFocus occurs and the data is sent back to the model. The delegate should then repaint with the new model data.
Could you expand on what you mean by a "live" control?
Why would you want to have an edit widget constantly open? I think a better way to do this would be to create a delegate which paints the normal view (i.e. for Qt::DisplayRole) in a way which you want. Assuming you create your subclass view correctly, the delegate should still update when the model changes.
If you really want to do what you're asking though, I suspect you might be able to by:
creating your own item delegate (subclassing QAbstractItemDelegate)
reimplement createEditor() to use a QLineEdit
then use the delegate's updateEditorGeometry()
Have a read of the Delegate Classes section of the Introduction to Model/View Programming though first. The Spin Box Delegate Example and Pixelator Example are worth studying too if you haven't already.

QML ListView Delegates z-order are always on top of everything else

I have a QML ListView with a custom widget (I call it PayloadOdometerRecord) as a delegate. This PayloadOdometerRecord contains a couple of other custom widgets (TextBox) that when clicked will change state and display a keyboard for user input.
The ListView works correctly until the user clicks one of these TextBox widgets to display the keyboard. The keyboard gets clipped by all of the delegates below the delegate that is displaying the keyboard.
When using this TextBox widget in a non-ListView, it works correctly...the keyboard is fully visible.
Does anyone have any idea why this is happening? My goal is to have a full-screen keyboard that shows up and is not covered by any other components.
Thanks!
Can you show some code? What is the parent of your keyboard component?
Z works only between siblings. The keyboard should be a child of the root element and have a higher z value than the other children of root to make it work.
If there are other components in front of the keyboard, you can try to set the keyboards z-property to a large value (e.g. 1000) → Qt Doc

Resources