前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Qml数字键盘

Qml数字键盘

作者头像
Qt君
发布2023-03-17 14:18:43
7760
发布2023-03-17 14:18:43
举报
文章被收录于专栏:跟Qt君学编程

双十一购物节为了避免剁手,给自己一个小目标,写个Qml数字键盘分享给大家。

1. Qml键盘代码

代码语言:javascript
复制
Rectangle {
    id: rootKeyboard
    property color backgroundColor: "#202120"    //default
    property color pressedButtonColor: "#00a0fc" // default
    property color normalButtonColor: "#303751" // default
    width: 350
    height: 205
    radius: 8
    color: backgroundColor

    GridView {
        id: gridView
        anchors.fill: parent
        cellWidth: width/4
        cellHeight: height/4
        interactive: false
        
        model: [
            "1", "2", "3", "Back",
            "4", "5", "6", "OK",
            "7", "8", "9", "-",
            ".", "0", "<", ">"
        ]
        
        delegate: Item {
            width: gridView.cellWidth
            height: gridView.cellHeight
            
            Rectangle {
                anchors.centerIn: parent
                width: parent.width - 6
                height: parent.height - 6
                radius: 8
                color: if (modelData == "Back")
                            return mouseArea.pressed ? normalButtonColor : "red"
                       else
                            return mouseArea.pressed ? pressedButtonColor : normalButtonColor
                
                Text {
                    anchors.centerIn: parent
                    text: modelData
                    font.pixelSize: 18
                    color: "white"
                }
                
                MouseArea {
                    id: mouseArea
                    anchors.fill: parent
                    // onClicked: 
                }
            }
        }
    }
}

2. 数字键盘配色代码

代码语言:javascript
复制
import QtQuick 2.0

Rectangle {
    anchors.fill: parent
    color: "gray"

    property Component numberKeyboard: ... //数字键盘组件
    
    Grid {
        x: 10
        y: 10
        
        rows: 3
        columns: 2
        
        spacing: 10
        
        Repeater {
            model: [
                { 
                    backgroundColor: "#202120", 
                    pressedButtonColor: "#00a0fc",
                    normalButtonColor: "#303751"
                },
                { 
                    backgroundColor: "#ECF0F1",
                    pressedButtonColor: "#F1C40F",
                    normalButtonColor: "#F39C12"
                },
                { 
                    backgroundColor: "#ECF0F1",
                    pressedButtonColor: "#7F8C8D",
                    normalButtonColor: "#BDC3C7"
                },
                { 
                    backgroundColor: "#ECF0F1",
                    pressedButtonColor: "#2980B9",
                    normalButtonColor: "#3498DB"
                },
                { 
                    backgroundColor: "#ECF0F1",
                    pressedButtonColor: "#2ECC71",
                    normalButtonColor: "#27AE60"
                },
                { 
                    backgroundColor: "#ECF0F1",
                    pressedButtonColor: "#9B59B6",
                    normalButtonColor: "#8E44AD"
                }
            ]
            
            Loader {
                id: loader
                sourceComponent: numberKeyboard
                Component.onCompleted: {
                    loader.item.backgroundColor = modelData.backgroundColor
                    loader.item.pressedButtonColor = modelData.pressedButtonColor
                    loader.item.normalButtonColor = modelData.normalButtonColor
                }
            }
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-11-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Qt君 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. Qml键盘代码
  • 2. 数字键盘配色代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档