componente qml

Componente

Personaliza un botón.
Nota: El nombre de archivo del componente Button.qml debe comenzar con letras mayúsculas; de lo contrario, no se puede llamar. no sé por qué
Inserte la descripción de la imagen aquí

//Button.qml
import QtQuick 2.0

Item{
    id:root
    signal clicked
    width: 116
    height: 26
    Rectangle {
        id: rect
        anchors.fill: parent
        //alias(别名)功能,它可以将内部嵌套的QML元素的属性导出到外⾯使⽤
        property alias text: label.text
        width: parent
        height: parent
        color: "lightsteelblue"
        border.color: "slategrey"
        Text {
            id: label
            anchors.centerIn: parent
            text: "Start"
        }
    }
    MouseArea {
        anchors.fill: parent
        onClicked: {
            root.clicked()
        }
    }
}
//compontents.qml
import QtQuick 2.12
import QtQuick.Window 2.12

Item {
    id: name
    //自定义Button组件,注意:Button.qml文件名要大写,小写调用不了,不知道为什么
    Button {
        id: button
        x:12; y:12
        onClicked: {
            status.text = "Button clicked!"
        }
    }
    Text {
        id: status
        x: 12; y:76
        width: 116; height: 26
        text: qsTr("waiting...")
        horizontalAlignment: Text.AlignHCenter
    }
}

Código fuente: https://github.com/sunlianqi/qml/tree/master/compontents

Supongo que te gusta

Origin blog.csdn.net/sinat_33859977/article/details/115037648
Recomendado
Clasificación