Details
-
Task
-
Resolution: Unresolved
-
P2: Important
-
None
-
None
-
None
Description
Background
WId is a handle to a native window, used by functions such as QWindow::wId() and QWindow::fromWinId().
Today, WId is an integer sequence number on wasm. Qt for WebAssembly did not have native windows in the beginning, but does now, which makes it possible to change WId to refer to the native html element.
Making this change could be source incompatible for user code which uses WId, but this should be acceptable since the current WId implementation does not have many use cases.
Qt is currently using the native type emscripten::val to refer to JS objects, for instance in QByteArray::fromEcmaUint8Array(). emscripten::val can also refer to html elements.
Typedefing WId to val is not straightforward: val is a references counted value type, existing code expects WId to be a pointer-like type (which is is on other platforms).
WId implementation options
1) WId is emscripten::val
val document = val::global("document");
val videoElement = document.call<val>("createElement", std::string("video"));
QWindow *videoWindow = QWindow::fromWinId(videoElement);
Existing code expects that WId is a pointer. Emscripten::val is missing:
- missing conversion from 0 / nullptr. (WId winId = 0 )
- missing conversion to bool (if (winId) { ... })
- missing QVariant support
- can it be used as a QHash key?
Could maybe be solved by using a wrapper type which implements the missing features, or by porting existing code to use helpers (e.g. if (isNullWId(winId))).
2) WId is emscripten::val *
With this option Q_OS_WASM WId is a pointer, like on other platforms.
2.1) User code keeps ownership of the val pointer
emscripten::val *videoElementCopy = new val(videoElement);
QWindow *videoWindow = QWindow::fromWinId(videoElement);
...
delete videoElementCopy; // delete when done with the window
2.2) Qt takes keeps ownership of the val pointer
QWindow *videoWindow = QWindow::fromWinId(new val(videoElement));
QWindow owns the allocated val object, and deletes in its destructor.