2014-04-30 62 views
2

爲什麼無法爲款式定義別名?例如款式別名

Button { 

    property alias color: theText.color 

    style: ButtonStyle { 
     label: Text { 
      id: theThext 
     } 
    } 
} 

給出了一個

QML無效別名參考無法找到ID爲theText

+0

排序相關的問題:http://stackoverflow.com/questions/14345552/how-to - 創建別名內中繼器,elemets – Mitch

回答

3

類似於this answer,這是因爲該alias是指該項目是動態加載。像Label這樣的組件類型就是:Components。它們是用於創建實際樣式Item的模板,它們實際上是使用Loader加載的。

1

這裏是我的解決方案(例如工作):

//MyCustomBtn.qml 
import QtQuick 2.0 
import QtQuick.Controls 1.2 
import QtQuick.Controls.Styles 1.2 

Button { 
    function setFontColor(fontColor){color_ = fontColor} 
    property string color_: "black" 

    style: ButtonStyle { 
     label: Text { 
      color: color_ 
      text: control.text 
     } 
    } 
} 

//MyTest.qml 
MyCustomBtn { 
    text: qsTr("Hello World") 
    anchors.horizontalCenter: parent.horizontalCenter 
    anchors.verticalCenter: parent.verticalCenter 

    Component.onCompleted: setFontColor("red") 
} 
0

在默認字體大小

Button { 
    property int fontPixelSize: 0 
    style: ButtonStyle { 
     label: Text { 
      font.pixelSize: fontPixelSize ? fontPixelSize : font.pixelSize 
      text: control.text 
     } 
    }