【QML】按钮 Button

import QtQuick.Controls 2.5

Button 从 AbstractButton 继承其 API。例如可以使用 AbstractButton API 设置文本(text)、显示图标(icon)和对点击(click)信号作出反应。

当用户激活按钮时,按钮会发出 clicked() 信号。需连接此信号以执行按钮的操作。Buttons also provide the signals canceled(), doubleClicked(), pressed(), released() and pressAndHold() for long presses.

属性

  • flat:(bool,默认 false)决定按钮是否平坦(除非按下或选中,否则平面按钮通常不会绘制背景)
  • highlighted:(bool,默认 false)决定按钮是否突出显示

Customizing Button

Button consists of two visual items: background and contentItem.

import QtQuick 2.12
import QtQuick.Controls 2.12

Button {
    id: control
    text: qsTr("Button")

    contentItem: Text {
        text: control.text
        font: control.font
        opacity: enabled ? 1.0 : 0.3
        color: control.down ? "#17a81a" : "#21be2b"
        horizontalAlignment: Text.AlignHCenter
        verticalAlignment: Text.AlignVCenter
        elide: Text.ElideRight
    }

    background: Rectangle {
        implicitWidth: 100
        implicitHeight: 40
        opacity: enabled ? 1 : 0.3
        border.color: control.down ? "#17a81a" : "#21be2b"
        border.width: 1
        radius: 2
    }
}

你可能感兴趣的:(【QML】按钮 Button)