天天看點

Qt Quick 常用控件:Button(按鈕)用法及自定義

1、簡介

Button 是很常見的控件

Button QML Type

Push-button that can be clicked to perform a command or answer a question. More...

Import Statement: import QtQuick.Controls 2.5

Since: Qt 5.7

Inherits: AbstractButton

Inherited By: RoundButton and ToolButton

根據以上可知,在 QML 中要使用 Buttoon,需要先導入控件庫 import QtQuick.Controls 2.5,使用其它控件也是一樣,都需要導入這個庫。

在界面上添加一個按鈕:

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5
 
Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
 
    Button{
        x:100  // 設定按鈕的橫坐标
        y:100  // 設定縱坐标
        text:"我是按鈕"   // 按鈕文本
 
        // 信号槽連接配接
        onClicked: {
            console.log("我被點選了")
        }
    }
}
           

上面隻設定了 Button 基本的文本屬性和 onClicked 事件處理,下面說說 QML 按鈕的其它屬性及用法。

2、屬性

  • flat

此屬性決定按鈕是否為扁平。除非按下或選中按鈕,否則通常不會繪制按鈕的背景。預設值為 false。

  • highlighted

此屬性決定按鈕是否突出顯示。可以突出顯示一個按鈕,以吸引使用者的注意。 它對鍵盤互動沒有影響。預設值為false。

  • enabled

設定是否使能。預設值為 true。

  • font.pointSize

設定字型大小。

3、信号槽連接配接

在 Button 的父類 AbstractButton 可以找到如下信号:

canceled()
clicked()
doubleClicked()
pressAndHold()
pressed()
released()
toggled()
           

按鈕信号槽寫法:on + 信号首字母大寫,例如下面的代碼,寫了一個點選事件,代碼如下:

// 信号槽連接配接,單擊信号
onClicked: {
	console.log("我被點選了")
}
QT開發交流+赀料君羊:714620761           

槽函數代碼的 3 種寫法

(1)調用外部 JS 函數;

(2)函數調用時大括号也可以不寫;

(3)用控件的 id 調用,例如給 Button 添加了一個屬性 id:myButoon。

Button{
    id:myButoon
    x:100 
    y:100
    text:"我是按鈕"

    // 信号槽連接配接,單擊信号
    onClicked: {
        console.log("我被點選了")
    }

    // 模拟外部JS函數
    function slotDouble(){
        console.log("我被輕按兩下了")
    }

    // 1.調用外部 JS 函數
    //onDoubleClicked: {
    //slotDouble();
    //}

    // 2.函數調用時大括号也可以不寫
    //onDoubleClicked: slotDouble()

    // 3.用控件的 id 調用
    //onDoubleClicked: myButoon.slotDouble()
}
           
QT開發交流+赀料君羊:714620761

4、自定義按鈕

先看下實作效果,未點選任何按鈕時:

Qt Quick 常用控件:Button(按鈕)用法及自定義

點選确定按鈕時:

Qt Quick 常用控件:Button(按鈕)用法及自定義

點選 OK 按鈕時:

Qt Quick 常用控件:Button(按鈕)用法及自定義

(1)純代碼方式,使用 color 屬性來設定點選前和點選後的字型、背景和邊框顔色以美化按鈕,MyButton.qml 的内容如下:

import QtQuick 2.9
import QtQuick.Controls 2.4

Button {
    id: root_Button
    font.pointSize: 16 // 設定字型大小

    property color clr_font: "#ffffff"
    property color clr_backNormal: "#498ff8"
    property color clr_backPress: "#0066FF"
    property color clr_boardNormal: "#498ff8"
    property color clr_boardPress: "#0066FF"

    // 設定按鈕文本
    contentItem: Text {
        id: text2
        text: root_Button.text
        font: root_Button.font
        opacity: enabled ? 1.0 : 0.3
        color: clr_font
        horizontalAlignment: Text.AlignHCenter
        verticalAlignment: Text.AlignVCenter
        elide: Text.ElideRight
    }

    // 設定按鈕背景
    background: Rectangle {
        implicitWidth: 100
        implicitHeight: 40
        opacity: enabled ? 1 : 0.3
        color: root_Button.down ? clr_backPress : clr_backNormal
        border.color: root_Button.down ? clr_boardPress : clr_boardNormal
        border.width: 1
        radius: 6
    }
}
           

(2)使用三張圖檔資源來實作按鈕的三态,ThreeIconButton.qml 的内容如下:

import QtQuick 2.0
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4

Button
{
    id: root_Button
    property string nomerPic: "qrc:/image/ok.png"
    property string hoverPic: "qrc:/image/ok1.png"
    property string pressPic: "qrc:/image/ok2.png"

    style: ButtonStyle {
        background:Rectangle{
            implicitHeight: root_Button.height
            implicitWidth:  root_Button.width

            color: "transparent"  // 設定背景透明,否則會出現預設的白色背景

            BorderImage {
                anchors.fill: parent
                source: control.hovered ? (control.pressed ? pressPic : hoverPic) : nomerPic;
            }
        }
    }
}
           

(3)使用 Rectangle 來重寫按鈕,同時顯示圖示和下方的提示文本,以及定義了點選和釋放信号槽,MyIconButton.qml 的内容如下:

import QtQuick 2.0

Rectangle {
    id: rec

    property alias img_src: icon.source
    property alias btn_txt: button.text

    property color clr_enter: "#dcdcdc"
    property color clr_exit: "#ffffff"
    property color clr_click: "#aba9b2"
    property color clr_release: "#ffffff"

    //自定義點選信号
    signal clickedLeft()
    signal clickedRight()
    signal release()

    width: 130
    height: 130
    radius: 10

    Image {
        id: icon
        width: 80
        height: 80
        source: "qrc:/camera.png"
        fillMode: Image.PreserveAspectFit
        clip: true
        anchors.top: parent.top
        anchors.right: parent.right
        anchors.left: parent.left
        anchors.margins: 10
    }

    Text {
        id: button
        text: qsTr("button")

        anchors.top: icon.bottom
        anchors.topMargin: 5
        anchors.horizontalCenter: icon.horizontalCenter
        anchors.bottom: icon.bottom
        anchors.bottomMargin: 5

        font.bold: true
        font.pointSize: 14
    }

    MouseArea {
        id: mouseArea
        anchors.fill: parent
        hoverEnabled: true

        //接受左鍵和右鍵輸入
        acceptedButtons: Qt.LeftButton | Qt.RightButton
        onClicked: {
            //左鍵點選
            if (mouse.button === Qt.LeftButton)
            {
                parent.clickedLeft()
//                console.log(button.text + " Left button click")
            }
            else if(mouse.button === Qt.RightButton)
            {
                parent.clickedRight()
            }
        }

        //按下
        onPressed: {
            color = clr_click
        }

        //釋放
        onReleased: {
            color = clr_enter
            parent.release()
        }

        //指針進入
        onEntered: {
            color = clr_enter
        }

        //指針退出
        onExited: {
            color = clr_exit
        }
    }
}
           

(4)實際調用代碼:

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.4

Window {
    visible: true
    width: 800
    height: 480
    title: qsTr("Hello World")

    Grid {
         columns : 3
         rows: 2
         spacing: 80
         anchors.centerIn: parent

         MyButton{
             id: btnCancle
             width: 100
             height: 50
             text: "取消"
             clr_font: "#498ff8"
             clr_backNormal: "#ffffff"
             clr_backPress: "#DEE4ED"
             clr_boardNormal: "#498ff8"
             clr_boardPress: "#498ff8"

         }

         MyButton{
             id: btnOk
             width: 100
             height: 50
             text: "确定"
         }

         ThreeIconButton {
             width: 150
             height: 60
             text: "OK"
         }

         MyIconButton {
             id: btn_camera
             img_src: "qrc:/image/camera.png";
             btn_txt: "相機"
             onClickedLeft: console.log(btn_txt + " Left button click")
         }

         MyIconButton {
             id: btn_video
             img_src: "qrc:/image/dianshiju.png";
             btn_txt: "視訊"
             onClickedLeft: console.log(btn_txt + " Left Button click")
         }

         MyIconButton {
             id: btn_audio
             img_src: "qrc:/image/music.png";
             btn_txt: "音樂"
             onClickedLeft: console.log(btn_txt + " Left Button click")
         }
    }
}           

繼續閱讀