0
我在QML
代碼定義爲Singleton
:不允許單身嵌套只讀屬性?
pragma Singleton
import QtQuick 2.0
QtObject {
/*
readonly property QtObject example1: QtObject {
readonly property int example1_1: 10
readonly property int example1_2: 105
readonly property int example1_3: 23
readonly property int example1_4: 105
readonly property int example1_5: 23
}
readonly property QtObject example2: QtObject {
property int example2_1: 10
property int example2_2: 105
property int example2_3: 23
property int example2_4: 105
property int example2_5: 23
}
property QtObject example3: QtObject {
readonly property int example3_1: 10
readonly property int example3_2: 105
readonly property int example3_3: 23
readonly property int example3_4: 105
readonly property int example3_5: 23
}*/
readonly property variant example4: {
"example4_1": 10,
"example4_2": 105,
"example4_3": 23,
"example4_4": 105,
"example4_5": 23
}
}
他們編譯,但是,example1
具有部分以紅色突出顯示(參見下圖)在Qt Creator
看似指示錯誤。
這似乎是a variant of this bug,只能用遞歸應用readonly
。上述錯誤在Qt 5.4中修復。總體而言,雖然我的代碼似乎有點呆滯,並且偶爾崩潰,所以我想知道使用QtObject
的方法是否可能會創建一些實際問題(我的實際代碼非常大,所以我不確定這是否是罪魁禍首,或者只是旁觀者)。
我知道example4
是最好的做法,因爲我宣佈了一系列readonly property
常見的原始類型值。
我的問題是:
- 是壞的語法高亮顯示的錯誤嗎?
- 在聲明
QtObject
和readonly property
值時是否存在任何基本問題? - 對於
example1
和example2
是否有任何潛在的問題也適用,即使它們被Qt Creator
視爲在語法上是正確的?
我使用的是Qt 5.7
和Qt Creator 4.0.2
。
是的,它編譯得很好......你的建議可能是最真實的過程......只是好奇,如果有一些潛在的理由懷疑語法。 :) –