• qml保姆级教程四:按钮组件


    💂 个人主页:pp不会算法v
    🤟 版权: 本文由【pp不会算法v】原创、在CSDN首发、需要转载请联系博主
    💬 如果文章对你有帮助、欢迎关注、点赞、收藏(一键三连)和订阅专栏哦

    QML系列教程

    QML教程一:布局组件

    AbstractButton

    这是Button, CheckBox, DelayButton, ItemDelegate, MenuBarItem, MenuItem, RadioButton, Switch, and TabButton的共同父类

    属性

    属性表格:

    属性描述默认值
    action按钮的动作。
    autoExclusive是否启用自动互斥。false
    autoRepeat当按钮被按下并保持按下时,是否重复触发 pressed()、released() 和 clicked() 信号。false
    autoRepeatDelay自动重复触发开始的延迟时间(毫秒)。300
    autoRepeatInterval自动重复触发的时间间隔(毫秒)。100
    checkable按钮是否可选中。false
    checked按钮是否已选中。false
    display按钮中图标和文本的显示方式。TextBesideIcon
    down按钮是否处于按下状态。undefined
    icon.name图标的名称。“”
    icon.source图标的源文件路径。“”
    icon.width图标的宽度。0
    icon.height图标的高度。0
    icon.color图标的颜色。
    implicitIndicatorHeight隐式指示器的高度。0
    implicitIndicatorWidth隐式指示器的宽度。0
    indicator按钮上的指示器项。
    pressX最后一次按下时的 x 坐标。
    pressY最后一次按下时的 y 坐标。
    pressed按钮是否处于物理按下状态。false
    text按钮的文本描述。“”

    信号

    信号名描述
    canceled当按钮在被按下状态下失去焦点或鼠标抓取时发出。
    clicked当用户通过触摸、鼠标或键盘与按钮进行交互时发出。
    doubleClicked当用户通过触摸或鼠标双击与按钮进行交互时发出。
    pressAndHold当用户通过触摸或鼠标长按与按钮进行交互时发出。
    pressed当用户通过触摸、鼠标或键盘按下与按钮进行交互时发出。
    released当用户通过触摸、鼠标或键盘释放与按钮进行交互时发出。
    toggled当用户通过触摸、鼠标或键盘切换可选中按钮的选中状态时发出。

    函数

    方法名描述
    toggle切换按钮的选中状态。

    普通按钮Button

    属性

    属性说明
    flat按钮是否为扁平样式。 flat属性决定按钮是否为扁平样式。扁平按钮通常只在被按下或选中时才会绘制背景。默认值为false
    highlighted按钮是否为高亮状态。 highlighted属性决定按钮是否为高亮状态。高亮状态可用于吸引用户注意力,对键盘交互没有影响。默认值为false

    示例

        Button {
            height: 50
            width: 80
            text: "普通按钮"
            anchors.centerIn: parent
    
            background: Rectangle {
                id: buttonBackground
                color: "pink"
                radius: 5
                border.width: 0
            }
    
            MouseArea {
                anchors.fill: parent
                hoverEnabled: true
    
                onEntered: {
                    buttonBackground.color ="#5FB0F3"// 鼠标悬停时的背景颜色
                }
    
                onExited: {
                    buttonBackground.color = "pink"  // 恢复正常的背景颜色
                }
                onClicked: {
                    console.log("点击了按钮")
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    在这里插入图片描述

    单选按钮RadioButton

    RadioButton提供了一个选项按钮,可以打开(选中)或关闭(未选中)。单选按钮通常用于从一组选项中选择一个选项。
    RadioButton从AbstractButton继承其API。例如,您可以使用AbstractButton
    API设置文本并对点击做出反应。单选按钮的状态可以使用选中的属性进行设置。
    默认情况下,单选按钮是自动独占的。在属于同一父项的单选按钮中,任何时候只能检查一个按钮;选中另一个按钮会自动取消选中先前选中的按钮。对于不共享公共父级的单选按钮,ButtonGroup可用于管理独占性。
    RadioDelegate类似于RadioButton,只是它通常用于视图中。

    示例

       ColumnLayout {
            RadioButton {
                checked: true //默认选中
                text: qsTr("First")
            }
            RadioButton {
                text: qsTr("Second")
            }
            RadioButton {
                text: qsTr("Third")
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述

    复选框CheckBox

    属性

    属性描述默认值
    checkState复选框的选中状态。
    nextCheckState确定用户交互切换复选框时下一个选中状态的回调函数。
    tristate复选框是否为三态复选框。false
    checkState 值描述
    Qt.Unchecked复选框未选中状态。
    Qt.PartiallyChecked复选框处于部分选中状态。仅当 tristate 启用时才使用此状态。
    Qt.Checked复选框已选中状态。

    示例

           Column {
                ButtonGroup {
                    id: childGroup
                    exclusive: false
                    checkState: parentBox.checkState
                }
    
                CheckBox {
                    id: parentBox
                    text: qsTr("Parent")
                    checkState: childGroup.checkState
                }
    
                CheckBox {
                    checked: true
                    text: qsTr("Child 1")
                    leftPadding: indicator.width
                    ButtonGroup.group: childGroup
                }
    
                CheckBox {
                    text: qsTr("Child 2")
                    leftPadding: indicator.width
                    ButtonGroup.group: childGroup
                }
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    在这里插入图片描述

    开关Switch

    属性

    属性名说明默认值
    position (只读)逻辑位置0
    visualPosition (只读)视觉位置0
    二者的区别 position只能是0或者1表示开关的关和开两种状态
    visulPosition表示你实际看到开关你的圆点所在的位置范围为0.0~1.0,

    示例

       ColumnLayout {
            Switch {
                text: qsTr("Wi-Fi")
    
            }
            Switch {
                text: qsTr("Bluetooth")
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述
    s

  • 相关阅读:
    计算机毕业设计之java+springboot基于vue的地方美食分享网站
    Mathcad的使用与设计
    前端需要去了解的nodejs知识(fs文件处理)
    Linux系统上使用SQLite
    【巨杉数据库】银行流水查询系统设计
    Android笔记: 设置PopupMenu宽度
    一致性哈希算法【图解理论 + 代码实现】
    宿舍管理微信小程序源码
    408-2015
    Vue3和Elment-Plus
  • 原文地址:https://blog.csdn.net/weixin_73548574/article/details/133578051