Qt for WebAssembly

WebAssembly 是允許在網頁沙盒執行代碼的二進製格式。這種格式幾乎和本機代碼一樣快,且現在所有主流 Web 瀏覽器都支持。

Qt for WebAssembly 是 平颱插件 ,允許構建可以被集成到網頁的 Qt 應用程序。它不需要安裝任何客戶端,並減少服務器端資源的使用率。

Qt for WebAssembly 快速入門

安裝 Emscripten

emscripten is a toolchain for compiling to asm.js and WebAssembly. It lets you run Qt on the web at near-native speed without plugins.

參考 Emscripten 文檔編製 for more information about checking out the Emscripten SDK and installing and activating Emscripten for your Qt version.

安裝後,路徑下應該有 Emscripten。采用以下命令校驗這:

em++ --version
					

Qt 的每個子版本均支持已知的良好 Emscripten 版本。在 Qt 版本的生命周期內會支持這種 Emscripten 版本。

已知的良好版本:

  • Qt 5.12: 1.38.16
  • Qt 5.13: 1.38.27 (multithreading: 1.38.30)
  • Qt 5.14: 1.38.27 (multithreading: 1.38.30)
  • Qt 5.15: 1.39.8

使用 emsdk 去安裝特定 emscripten versions. For example, to install it for Qt 5.15, enter:

  • ./emsdk install 1.39.8
  • ./emsdk activate --embedded 1.39.8

安裝後,路徑下應該有 Emscripten。采用以下命令校驗這:

em++ --version
					
					

下載二進製

使用 Qt 帳戶可以在 Downloads 章節下載二進製構建。

Supported Qt modules

The following modules are supported by Qt for WebAssembly:

The following modules do not work:

Other modules may work, but are not supported.

構建 Qt 從源

另外,在 Downloads 章節可以下載 Qt 源代碼,然後從它構建 Qt。

把 Qt 配置成交叉編譯構建為 wasm-emscripten platform. This will implicitly set the "-static" and "-no-feature-thread" options. Pass the -compile-examples option to enable examples.

./configure -xplatform wasm-emscripten -nomake examples -prefix $PWD/qtbase
					

在 Windows,確保您有 MinGW sed 在您的 PATH 並配置如下:

configure -no-warnings-are-errors -xplatform wasm-emscripten -platform win32-g++ -nomake examples -prefix %CD%\qtbase
					

構建所需模塊:

make module-qtbase module-qtdeclarative [other modules]
					

The Qt build is a static build, and does not support threads by default. Configure with -feature-thread to enable thread support.

構建並運行應用程序

$ /path/to/qt-wasm/qtbase/bin/qmake
$ make
					

這生成下列文件:

生成的文件 簡要描述
app.html HTML 容器
qtloader.js 用於加載 Qt APP 的 JS API
app.js 用於加載 Qt APP 的 JS API
app.wasm 二進製 Emscripten APP

當部署 APP 時,通常在服務器側處理壓縮。我們推薦壓縮 WASM 二進製,因為這通常會將二進製大小減少 50%。

測試 運行應用程序

可以按以下方式測試-運行應用程序:

/path/to/emscripten/emrun --browser=firefox appname.html
					
					

一些範例

局限性

The most important limitation is that the Qt build is static.

瞭解進一步信息,參考 Qt WebAssembly Platform Notes .

外部資源

許可

Qt for WebAssembly 在商業許可下是可用的來自 Qt 公司 。此外,它是可用的根據 GNU GPL (一般公共許可) 第 3 版 。見 Qt 許可 進一步瞭解細節。

另請參閱 WebAssembly 資源站點 , Qt for WebAssembly 快速入門 ,和 采用 WebGL 和 WebAssembly 的遠程 UI .