Question

Why cannot define aliases for Styles? For instance

Button {

    property alias color: theText.color

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

gives an

qml invalid alias reference unable to find id for theText

Was it helpful?

Solution

Similarly to this answer, it's because the item that the alias refers to is loaded dynamically. Style components like Label are just that: Components. They are templates that are used to create the actual style Items which are actually loaded with Loader.

OTHER TIPS

here is my solution (working example):

//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")
}

With default font size

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

My solution is to get control's property just like the text. You can even save this style to separate style file and set the control's labelColor property.

Button {
    property string labelColor: "yellow"
    text: "hede"
    style: ButtonStyle {
        label: Text {
            color: control.labelColor
            text: control.text
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top