A Qt Quick TextField backed by an <input>

Qt for WebAssembly draws a TextField as pixels and takes typing through one hidden input element that it shares between all its fields. This page loads a Qt Quick 6.10 app with three pairs of fields. In each pair the first is Qt's own TextField. The second is a QML component with the same background and focus border, whose text, caret and selection are a real <input> drawn into Qt's scene. Load the app, tap each field and type. The tables and logs under the app record what the browser was given for each field.

Qt Quick 6.10 for WebAssembly. It isn't loaded until you ask.

The app isn't loaded.

Show the code a QML text field that is an input element
  1. The inputs. Each has the type, name and autocomplete that a form on a web page would give it.
  2. QML: DomTextField. Qt draws the background and border of the Basic style's TextField. A DomItem fills it.
  3. QML: the window. A Label and a Button bind to text of both kinds of field in the same way.
  4. C++: the two-way text property, focus and Tab.
  5. JavaScript inside the C++ file: read and write the value, focus the input.
  6. Move the input into Qt's canvas and send its focus, blur and Tab key to the item.
  7. Stop Qt taking focus back.
  8. Copy the input's picture into a texture that Qt Quick's scene graph draws.
  9. Tell the browser where Qt drew the item.
  10. Start Qt and load a stylesheet for the inputs inside Qt's shadow root.
  11. CSS for the inputs.
  12. The event log. Listeners in the capture phase on window.
  13. All of domitem.cpp.

What the browser is given for each field

The page writes these from real events. A password field's keys and text are logged as lengths only. On a phone: tap a field, wait for the keyboard, type a few characters with a Japanese, Chinese or Korean keyboard if you have one, and look for an autofill suggestion above the keyboard. Then tap "Copy the log".

Qt TextField: the element Qt focuses

elementTap a Qt TextField.

DomTextField: the focused <input>

elementTap a DomTextField.

How it works

Qt Quick renders the window into one WebGL canvas, which Qt creates inside a shadow root. DomItem is a QQuickItem written for /native-ui/. It moves an element into that canvas, copies the element's picture into a texture with gl.texElementSubImage2D() on each paint event, and passes the item's matrix to canvas.updateElementGeometry(), so the browser's box for the element is where Qt drew it. The browser handles taps, keys, composition and autofill on the element as it does for any input.

DomTextField.qml is the Basic style's TextField background (a Rectangle with a 1 px border, 2 px in the highlight colour when focused) with a DomItem inside it. The copy of DomItem on this page adds three things. Its text property reads element.value on every input event and assigns it when QML writes the property. Its domFocus property follows the element's focus and blur events. It handles the Tab key in the element by asking Qt for nextItemInFocusChain(), and it focuses the element when Qt's own Tab reaches the item.

Qt's own TextField has no element where it is drawn. When one gets focus, Qt focuses a hidden <input class="qt-window-input-element"> that it keeps at the top left corner of the window, and turns that element's events into Qt key and input method events. The page's log listens in the capture phase on window, so it records the events for both kinds of field before Qt or DomItem handle them.

Live checks

The checks load a second copy of the app (23 MB) in a small frame, so they don't touch the one above. In that frame focus() is recorded and not run, so the checks read which element Qt would focus without moving focus on this page.

ResultExperimentDetail

Findings

Source: native/qt-textfield. The binary is statically linked against the open-source edition of Qt 6.10.3 (LGPL-3.0 and GPL-3.0). Its complete source is that folder plus Qt from download.qt.io.