1. 程式人生 > >qml canvas3D 載入介面速度非常慢的解決方法

qml canvas3D 載入介面速度非常慢的解決方法

剛開始使用QtCanvas3D, canvas3D是類似於html的架構,2d部分用qml自帶控制元件,3d部分用canvas3D繪製。 但是在使用的過程中遇到一個問題,每次我的頁面跳轉到含有canvas3D的頁面時,就會非常卡。 經過一番查詢,我發現問題出在

import "glcode.js" as GLCode

這句話。

如果不註釋掉這句話,就算qml程式碼裡面沒有canvas3D模組,載入速度也是一樣的慢。如果註釋掉,就會很快。進一步我發現,問題是出在這個檔案內部的

Qt.include("THREE.js")
這句話。

於是上網搜了一下,import的含義:

http://doc.qt.io/qt-5/qtqml-javascript-imports.html

文中說道:

When a JavaScript file is imported, it must be imported with a qualifier. The functions in that file are then accessible from the importing script via the qualifier (that is, as Qualifier.functionName(params)). Sometimes it is desirable to have the functions made available in the importing context without needing to qualify them, and in this circumstance the 

Qt.include() function may be used to include one JavaScript file from another. This copies all functions from the other file into the current file's namespace, but ignores all pragmas and imports defined in that file.

這段話是說Qt.include() 會把檔案的所有內容全部拷貝到你inlcude的那個檔案裡面,來實現不需要qualifier就可以直接引用。。。這麼低效率的方式也不知道qt是怎麼想出來的,並且還是預設方式,如果檔案的大小比較小還可以,如果超過1mb,每次開啟頁面都複製一份,實在是太慢了。

解決方式就是用import的方式吧檔案匯入:

.import Qt.test 1.0 as JsQtTest

這樣就可以了。缺點就是你需要指定一個類似於名稱空間的東西,然後你使用函式的時候需要帶上這個名字。