2013-06-26 30 views
10

de kodu yeniden: QML programlamaBunu nasıl QML kod parçası var QML

Column { 
     spacing: units.gu(2) 
     anchors { 
      fill: parent 
      centerIn: parent 
     } 
     Row { 
      spacing: units.gu(4) 
      ... 
     } 
     Row { 
      spacing: units.gu(4) 
      ... 
     } 
     Row { 
      spacing: units.gu(4) 
      ... 
     } 
     Row { 
      spacing: units.gu(4) 
      ... 
     } 
    } 

Hakkında en iyi uygulamaları, nasıl ortak eleman yinelenen özelliklerini önlemek için kodu yeniden? Yukarıdaki örnekte olduğu gibi, Satırlar "boşluk: units.gu (4)" dan kaçının.

cevap

10

Kodunuzu ayrı bir qml dosyasına koyun ve bu dosya adını bir öğe olarak kullanın. Örneğin.

Column { 
     spacing: units.gu(2) 
     anchors { 
      fill: parent 
      centerIn: parent 
     } 
     MyCustomRow{ 

     } 
     MyCustomRow{ 

     } 
     MyCustomRow{ 

     } 
     MyCustomRow{ 

     } 
    } 

Infact bir repeater kullanabilirsiniz:

// diğer QML dosyasında Sonra

Row { 
     spacing: units.gu(4) 
     ... 
    } 

MyCustomRow.qml

File

Column 
{ 
      spacing: units.gu(2) 
      anchors 
      { 
       fill: parent 
       centerIn: parent 
      } 

      Repeater 
      { 
       model : 5 
       MyCustomRow 
       { 

       } 
      } 
} 

Edit:

(yorumunda sorulan gibi) tek bir dosyada yapıyor için

Eğer Loader elemanı ile birlikte QML Component eleman kullanabilirsiniz:

Column { 
     spacing: units.gu(2) 
     anchors { 
      fill: parent 
      centerIn: parent 
     } 


     Component 
     { 
     id : myCustomRow 
     Row 
     { 
      spacing: units.gu(4) 
      ... 
     } 
     } 

     Loader { 
     sourceComponent : myCustomRow 

     } 
     Loader { 
     sourceComponent : myCustomRow 

     } 
     Loader { 
     sourceComponent : myCustomRow 

     } 
     Loader { 
     sourceComponent : myCustomRow 

     } 
    } 
+0

İlginç. Ve bunu aynı dosyada nasıl yapmalı? –

+1

@ayr_ton Bir düzenleme yaptım. Kontrol. –