2016-09-06 51 views
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看似指示錯誤。

enter image description here

這似乎是a variant of this bug,只能用遞歸應用readonly。上述錯誤在Qt 5.4中修復。總體而言,雖然我的代碼似乎有點呆滯,並且偶爾崩潰,所以我想知道使用QtObject的方法是否可能會創建一些實際問題(我的實際代碼非常大,所以我不確定這是否是罪魁禍首,或者只是旁觀者)。

我知道example4是最好的做法,因爲我宣佈了一系列readonly property常見的原始類型值。

我的問題是:

  1. 是壞的語法高亮顯示的錯誤嗎?
  2. 在聲明QtObjectreadonly property值時是否存在任何基本問題?
  3. 對於example1example2是否有任何潛在的問題也適用,即使它們被Qt Creator視爲在語法上是正確的?

我使用的是Qt 5.7Qt Creator 4.0.2

回答

1

您的示例在運行時是否正常工作?如果是的話,這可能是一個Qt Creator的bug。最重要的是當你運行它時,你的程序能夠正常工作,而不是Qt Creator正確顯示的東西。因此,如果它指出錯誤的東西是正確的,那麼測試它並且使用Qt Creator。

否則,我會親自在任何地方使用JSON對象,就像您的example4屬性一樣。

+0

是的,它編譯得很好......你的建議可能是最真實的過程......只是好奇,如果有一些潛在的理由懷疑語法。 :) –