2015-01-27 12 views
6

Sto cercando di eseguire proprietà annidate come "font.family" o "anchors.fill", ma non posso inizializzarle in modo normale perché stampa "Impossibile assegnare a una proprietà inesistente". Invece sono costretto a usare il metodo Component.onCompleted. Cosa c'è che non va?Come creare proprietà raggruppate/annidate?

MyButtonStyling.qml:

import QtQml 2.1 

QtObject 
{ 
    property QtObject background: QtObject 
    { 
     property color pressed: "#CCCCCC" 
     property color enabled: "#666666" 
     property color disabled: "#555555" 
    } 
} 

main.qml:

import QtQuick 2.0 

Item 
{ 
    width: 400 
    height: 300 
    MyButton 
    { 
     text: "TEST" 
     styling: MyButtonStyling 
     { 
      //background.enabled: "#1B2E0A" //Cannot assign to non-existent property "enabled" 
      Component.onCompleted: 
      { 
       background.enabled = "#1B2E0A" //Works 
      } 
     } 
    } 
} 

MyButton.qml:

import QtQuick 2.0 
import QtQuick.Controls 1.0 
import QtQuick.Controls.Styles 1.0 

Button 
{ 
    property QtObject styling: MyButtonStyling {} 

    implicitWidth: 80 
    implicitHeight: 80 

    style: ButtonStyle 
    { 
     background: Item 
     { 
      Rectangle 
      { 
       anchors.fill: parent 
       color: control.pressed ? styling.background.pressed : control.enabled ? styling.background.enabled : styling.background.disabled 
      } 
     } 
    } 
} 
+1

Può chiarire esattamente quello che stai con l'intenzione di realizzare con il codice di cui sopra? Potrebbe esserci un modo migliore per raggiungere questo obiettivo, ma non sono del tutto chiaro su cosa stai cercando di fare. – MrEricSir

risposta

10

provare a sostituire il vostro nidificato QtObject con un file QML. Ad esempio, l'ho sostituito con BackgroundTheme.qml. In questo modo, la proprietà (che può essere correttamente definita "proprietà raggruppata") funziona correttamente, in un binding e senza errori.

BackgroundTheme.qml

import QtQuick 2.0 

QtObject { 
    property color pressed: "#CCCCCC" 
    property color enabled: "#666666" 
    property color disabled: "#555555" 
} 

MyButtonStyling.qml

import QtQuick 2.0 

QtObject {  
    property BackgroundTheme background: BackgroundTheme {} 
} 
Problemi correlati