DOM elements in a Qt Widgets window
Qt Quick renders with WebGL, and an earlier page gave it DOM elements as textures. Qt Widgets, the older and more
widely used Qt toolkit, paints every window in software and copies the changed pixels to a 2D canvas with putImageData().
This page loads a Qt Widgets 6.10 app and draws real elements into the window's canvas with drawElementImage() after each of
those copies. Load the app, then tap a field to type, scroll the window, move the slider, or open the Qt combo box above the fields.
DOM text with a link: مرحبا, 你好, 👋🏽.
Qt 6.10 for WebAssembly. It isn't loaded until you ask.
The app isn't loaded.
Show the code a DOM element in a Qt Widgets layout
- The elements. They start outside Qt's canvas, which doesn't exist yet.
- C++: the window. A
DomWidgetis added to aQVBoxLayoutlike any widget. - C++: the widget. It paints a background and reports its rectangle and its visible part.
- JavaScript inside the C++ file: draw after Qt's flush.
putImageData()on Qt's context is wrapped. - Move the elements into Qt's canvas and ask Qt to repaint a widget when the browser repaints its element.
- Record where the widget is.
- Start Qt.
- CSS for the elements.
- All of domwidget.cpp.
Live checks
The checks load a second copy of the app (14 MB) in a small frame, so they don't touch the one above.
| Result | Experiment | Detail |
|---|
Findings
- Yes, with one wrapped method. Qt for WebAssembly paints a Qt Widgets window with its raster engine and copies the changed rectangles to a 2D canvas with
putImageData(). The page wrapsputImageData()on that canvas's context and callsdrawElementImage()for each element straight after it. Qt 6.10.3 is unchanged. Measured in Chromium 157. - No geometry call is needed. In a 2D canvas
drawElementImage()also sets the hit box, so a tap lands on the field where Qt laid out its widget. An element whose widget is scrolled out isn't drawn and is madeinert. - The canvas is Qt's, inside a shadow root. Qt creates
canvas.qt-window-canvasin the shadow root of a child of the container. The page adds thelayoutsubtreeandcontent="drawable"attributes after Qt made it and appends the elements to it. Styles from the page don't reach the shadow root, so the page copies each element's computed style inline first. - Layout goes both ways through
heightForWidth(). Qt's layout gives the widget a width inresizeEvent(), the element takes that width, and the widget answersheightForWidth()with the element'soffsetHeight. - A Qt popup covers the fields with no guard code. The
QComboBoxlist is a second Qt window, which is a second canvas above the first one, so the browser's own hit testing gives the popup the tap. - Drawing after the flush doesn't loop.
drawElementImage()outside a paint event doesn't cause a paint event. A change in an element firespaint, the widget callsupdate(), Qt flushes and the elements are drawn again: one paint event, two flushes. - Qt overwrites the element on every flush of its rectangle. The widget fills its background in
paintEvent(), so the element has to be drawn again after every flush. The page draws all of them after each one. - Touch scrolling is Qt's, and stops at an element.
QScroller::grabGesture()on the viewport makes a touch drag scroll the area. A drag that starts on a DOM element doesn't scroll it, because the element stops the event before Qt's listeners. - Cost: 14 MB. The WebAssembly file is 14.1 MB before compression, so the app loads after a tap on this page.
Source: native/qt-widgets-dom. 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.