Button表示一个推按钮控件,用户可以按下或单击它。
import QtQuick
import QtQuick.Window
import QtQuick.ControlsWindow {id: winwidth: 800height: 600visible: truetitle: qsTr("Hello World")Button {id: btnwidth: 200height: 100anchors.centerIn: parenttext: qsTr("This is a Button")}
}
自定义按钮的文本样式:
import QtQuick
import QtQuick.Window
import QtQuick.ControlsWindow {id: winwidth: 800height: 600visible: truetitle: qsTr("Hello World")Button {id: btnwidth: 200height: 100anchors.centerIn: parenttext: qsTr("This is a Button")//自定义文本样式contentItem: Text {text: btn.textfont: btn.fontopacity: enabled ? 1.0 : 0.3color: btn.down ? "#17a81a" : "#21be2b"horizontalAlignment: Text.AlignHCenterverticalAlignment: Text.AlignVCenterelide: Text.ElideRight}}
}
自定义按钮背景样式:
import QtQuick
import QtQuick.Window
import QtQuick.ControlsWindow {id: winwidth: 800height: 600visible: truetitle: qsTr("Hello World")Button {id: btnwidth: 200height: 100anchors.centerIn: parenttext: qsTr("This is a Button")//自定义文本样式contentItem: Text {text: btn.textfont: btn.fontopacity: enabled ? 1.0 : 0.3color: btn.down ? "#17a81a" : "#21be2b"horizontalAlignment: Text.AlignHCenterverticalAlignment: Text.AlignVCenterelide: Text.ElideRight}//自定义按钮背景background: Rectangle {implicitWidth: 100 //背景默认宽度implicitHeight: 40 //背景默认高度opacity: enabled ? 1 : 0.3 //背景不透明度border.color: btn.down ? "#17a81a" : "#21be2b" //背景边框颜色border.width: 1 //背景边框宽度radius: 2 //背景圆角}}
}
自定义图片按钮:
import QtQuick
import QtQuick.Window
import QtQuick.ControlsWindow {id: winwidth: 800height: 600visible: truetitle: qsTr("Hello World")Button {id: btnwidth: 200height: 100anchors.centerIn: parenttext: qsTr("This is a Button")//自定义文本样式contentItem: Text {text: btn.textfont: btn.fontopacity: enabled ? 1.0 : 0.3color: btn.down ? "#17a81a" : "#21be2b"horizontalAlignment: Text.AlignHCenterverticalAlignment: Text.AlignVCenterelide: Text.ElideRight}//自定义按钮背景background: Image {opacity: enabled ? 1 : 0.3 //背景不透明度source: "qrc:/img.jpg"}}
}
按钮事件:
import QtQuick
import QtQuick.Window
import QtQuick.ControlsWindow {id: winwidth: 800height: 600visible: truetitle: qsTr("Hello World")Button {id: btnwidth: 200height: 100anchors.centerIn: parenttext: qsTr("This is a Button")//自定义文本样式contentItem: Text {text: btn.textfont: btn.fontopacity: enabled ? 1.0 : 0.3color: btn.down ? "#17a81a" : "#21be2b"horizontalAlignment: Text.AlignHCenterverticalAlignment: Text.AlignVCenterelide: Text.ElideRight}//自定义按钮背景background: Image {opacity: enabled ? 1 : 0.3 //背景不透明度source: "qrc:/img.jpg"}onCanceled: {//当按钮在被按下时失去鼠标抓取,或者当它发出released信号//但鼠标光标不在按钮内时,就会发出这个信号。}onClicked:{//用户通过触摸、鼠标或键盘交互单击按钮}onDoubleClicked:{//用户通过触摸或鼠标交互双击按钮}onPressAndHold:{//用户通过触摸或鼠标交互按下按钮。//当启用autoRepeat时,它不会被触发}onPressed:{//用户通过触摸、鼠标或键盘交互按下按钮}onReleased:{//用户通过触摸、鼠标或键盘交互释放按钮}onToggled:{}
}