【Qt QML入门】Button组件

Button表示一个推按钮控件,用户可以按下或单击它。

import QtQuick
import QtQuick.Window
import QtQuick.Controls

Window {
    id: win
    width: 800
    height: 600
    visible: true
    title: qsTr("Hello World")

    Button {
        id: btn
        width: 200
        height: 100
        anchors.centerIn: parent
        text: qsTr("This is a Button")
    }
}

自定义按钮的文本样式:

import QtQuick
import QtQuick.Window
import QtQuick.Controls

Window {
    id: win
    width: 800
    height: 600
    visible: true
    title: qsTr("Hello World")

    Button {
        id: btn
        width: 200
        height: 100
        anchors.centerIn: parent
        text: qsTr("This is a Button")
        //自定义文本样式
        contentItem: Text {
            text: btn.text
            font: btn.font
            opacity: enabled ? 1.0 : 0.3
            color: btn.down ? "#17a81a" : "#21be2b"
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            elide: Text.ElideRight
        }
    }
}

自定义按钮背景样式:

import QtQuick
import QtQuick.Window
import QtQuick.Controls

Window {
    id: win
    width: 800
    height: 600
    visible: true
    title: qsTr("Hello World")

    Button {
        id: btn
        width: 200
        height: 100
        anchors.centerIn: parent
        text: qsTr("This is a Button")
        //自定义文本样式
        contentItem: Text {
            text: btn.text
            font: btn.font
            opacity: enabled ? 1.0 : 0.3
            color: btn.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: btn.down ? "#17a81a" : "#21be2b"   //背景边框颜色
            border.width: 1       //背景边框宽度
            radius: 2             //背景圆角
        }
    }
}

自定义图片按钮:

import QtQuick
import QtQuick.Window
import QtQuick.Controls

Window {
    id: win
    width: 800
    height: 600
    visible: true
    title: qsTr("Hello World")

    Button {
        id: btn
        width: 200
        height: 100
        anchors.centerIn: parent
        text: qsTr("This is a Button")
        //自定义文本样式
        contentItem: Text {
            text: btn.text
            font: btn.font
            opacity: enabled ? 1.0 : 0.3
            color: btn.down ? "#17a81a" : "#21be2b"
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            elide: Text.ElideRight
        }
        //自定义按钮背景
        background: Image {
            opacity: enabled ? 1 : 0.3        //背景不透明度
            source: "qrc:/img.jpg"
        }
    }
}

按钮事件:

import QtQuick
import QtQuick.Window
import QtQuick.Controls

Window {
    id: win
    width: 800
    height: 600
    visible: true
    title: qsTr("Hello World")

    Button {
        id: btn
        width: 200
        height: 100
        anchors.centerIn: parent
        text: qsTr("This is a Button")
        //自定义文本样式
        contentItem: Text {
            text: btn.text
            font: btn.font
            opacity: enabled ? 1.0 : 0.3
            color: btn.down ? "#17a81a" : "#21be2b"
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            elide: Text.ElideRight
        }
        //自定义按钮背景
        background: Image {
            opacity: enabled ? 1 : 0.3        //背景不透明度
            source: "qrc:/img.jpg"
        }

        onCanceled: {
            //当按钮在被按下时失去鼠标抓取,或者当它发出released信号
            //但鼠标光标不在按钮内时,就会发出这个信号。
        }

        onClicked:{
            //用户通过触摸、鼠标或键盘交互单击按钮
        }

        onDoubleClicked:{
            //用户通过触摸或鼠标交互双击按钮
        }

        onPressAndHold:{
            //用户通过触摸或鼠标交互按下按钮。
            //当启用autoRepeat时,它不会被触发
        }

        onPressed:{
            //用户通过触摸、鼠标或键盘交互按下按钮
        }

        onReleased:{
            //用户通过触摸、鼠标或键盘交互释放按钮
        }

        onToggled:{
    }
}

  • 15
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_君莫笑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值