QML自定义时间选择器

在进行使用qml编程过程中,需要实现日期时间的选择。
但是qml官方只提供日期控件,因此自己定义了时间控件
注意 QtQuick 2.9不要降低版本
否则会有内部的信号处理器无法使用

import QtQuick 2.9
import QtQuick.Controls 2.3

Rectangle
{
    property int hour: 0;
    property int minute: 0;
    property string hour_str: hour_str_area.text;
    property string minute_str: minute_str_area.text;
    width: 100;
    height: 140;
    color: "white";
    Rectangle
    {
        id: shi;
        x: 10;
        y: 10;
        width: 30;
        height: 30;
        Text {
            text: "时";
            anchors.centerIn: parent;
            font.pointSize: 20;
        }
    }
    Rectangle
    {
        id: add1;
        anchors.top: shi.bottom;
        anchors.left: shi.left;
        anchors.right: shi.right;
        height: shi.height;
        color: "red";
        Text
        {
            text: qsTr("+");
            font.pointSize: 20;
            anchors.centerIn: parent;
        }
        MouseArea
        {
            anchors.fill: parent;
            onClicked:
            {
                hour++;
                if(hour>23)
                    hour=0;
                if(hour<10)
                    hour_str_area.text="0"+hour
                else
                    hour_str_area.text=hour;
            }
        }
    }
    Rectangle
    {
        id: hour_show;
        anchors.top: add1.bottom;
        anchors.left: add1.left;
        anchors.right: add1.right;
        height: hour_str_area.height;
        TextInput {
            id: hour_str_area
            text: hour;
            validator: IntValidator{bottom: 0; top: 23;}
            font.pointSize: 20;
            anchors.centerIn: parent;
            onTextEdited:
            {
                hour=text;
            }
        }
    }
    Rectangle
    {
        id: subtract1;
        anchors.left: add1.left;
        anchors.right: add1.right;
        anchors.top: hour_show.bottom;
        height: add1.height;
        color: add1.color
        Text
        {
            text: qsTr("-");
            font.pointSize: 20;
            anchors.centerIn: parent;
        }
        MouseArea
        {
            anchors.fill: parent;
            onClicked:
            {
                hour--;
                if(hour<0)
                    hour=23;
                if(hour<10)
                    hour_str_area.text="0"+hour
                else
                    hour_str_area.text=hour;


            }
        }
    }
    Rectangle
    {
        id: fen;
        anchors.left: shi.right;
        anchors.leftMargin: 10;
        anchors.top: shi.top;
        height: shi.height;
        width: shi.width;
        Text {
            text: "分";
            anchors.centerIn: parent;
            font.pointSize: 20;
        }
    }


    Rectangle
    {
        id: add2;
        anchors.left: fen.left;
        anchors.right: fen.right;
        anchors.top: fen.bottom;
        height: add1.height;
        color: add1.color
        Text
        {
            text: qsTr("+");
            font.pointSize: 20;
            anchors.centerIn: parent;
        }
        MouseArea
        {
            anchors.fill: parent;
            onClicked:
            {
                minute++;
                if(minute>59)
                    minute=0;
                if(minute<10)
                    minute_str_area.text="0"+minute;
                else
                    minute_str_area.text=minute;
            }
        }
    }


    Rectangle
    {
        id: minute_show;
        anchors.top: hour_show.top;
        anchors.left: fen.left;
        anchors.right: fen.right;
        anchors.bottom: hour_show.bottom;
        TextInput {
            id: minute_str_area
            text: minute;
            validator: IntValidator{bottom: 0; top: 59;}
            font.pointSize: 20;
            anchors.centerIn: parent;
            onTextEdited:
            {
                minute=text;
            }
        }
    }
    Rectangle
    {
        id: subtract2;
        anchors.top: subtract1.top;
        anchors.bottom: subtract1.bottom;
        anchors.left: fen.left;
        anchors.right: fen.right;
        color: subtract1.color;
        Text
        {
            text: qsTr("-");
            font.pointSize: 20;
            anchors.centerIn: parent;
        }
        MouseArea
        {
            anchors.fill: parent;
            onClicked:
            {
                minute--;
                if(minute<0)
                    minute=59;
                if(minute<10)
                    minute_str_area.text="0"+minute;
                else
                    minute_str_area.text=minute;
            }
        }
    }
}

以上代码遵循WTFPL协议

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004Copyright © 2004 Sam Hocevar <sam@hocevar…net>Everyone is permitted to copy and distribute verbatim or modifiedcopies of this license document, and changing it is allowed as longas the name is changed…DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSETERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION0. You just DO WHAT THE FUCK YOU WANT TO.

### 创建或使用自定义 QML 时间选择组件QML 中创建或使用自定义时间选择组件可以通过组合现有的基础控件来实现。通过利用 `SpinBox` 或者滑动条 (`Slider`) 来表示小时、分钟和秒数,可以构建一个灵活的时间选择。 以下是基于 QML自定义时间选择的一个简单示例: ```qml import QtQuick 2.15 import QtQuick.Controls 2.15 ApplicationWindow { visible: true width: 400 height: 300 title: "Custom Time Picker" Column { anchors.centerIn: parent spacing: 20 Row { spacing: 10 SpinBox { id: hourSpinner from: 0 to: 23 value: 12 width: 80 suffix: ":" } SpinBox { id: minuteSpinner from: 0 to: 59 value: 0 width: 80 suffix: ":" } SpinBox { id: secondSpinner from: 0 to: 59 value: 0 width: 80 } } Button { text: "Set Time" onClicked: { console.log(`Time set to ${hourSpinner.value}:${minuteSpinner.value}:${secondSpinner.value}`) } } Label { text: "Selected Time: " + (hourSpinner.value).toString().padStart(2, '0') + ":" + (minuteSpinner.value).toString().padStart(2, '0') + ":" + (secondSpinner.value).toString().padStart(2, '0') font.pixelSize: 20 } } } ``` 在这个例子中,三个 `SpinBox` 控件分别用于设置小时、分钟和秒钟[^1]。这些控件允许用户输入范围内的数值,并提供了一个友好的界面来进行调整。当点击按钮时,控制台会打印当前选定的时间值。 如果需要更复杂的逻辑或者样式设计,则可以根据需求进一步扩展此基本框架。例如,可以引入动画效果使用户体验更加流畅,也可以增加验证机制防止非法输入。 #### 自定义组件封装 为了提高代码重用性和模块化程度,还可以将上述功能封装成独立的 QML 组件文件(如 `CustomTimePicker.qml`),以便在整个项目中多次调用而无需重复编写相同逻辑。 --- ###
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值