QML 仿手机时间滚筒控件

效果:共3中模式  "Date" | "Time" | "DateTime"

main.qml

    MouseArea{anchors.fill: parentonClicked: {dlg.exec(Qt.formatDateTime(new Date(), "yyyy-MM-dd hh:mm:ss"));}}DateTimeDialog{id: dlgtext: "修改系统时间"type: "DateTime"onAccepted: {console.log(getText("yyyy-MM-dd hh:mm:ss"));console.log(getText("yyyy-MM-dd"));console.log(getText("hh:mm:ss"));}}

DateTimeDialog.qml

import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12import "./" as MCDialog {id: rootimplicitHeight: 350implicitWidth: 600padding: 0anchors.centerIn: parentclosePolicy: Popup.CloseOnEscapebackground: Rectangle{anchors.fill: parentcolor: "#696969"}modal: trueproperty string text: ""property string type: "DateTime"onTypeChanged: {root.implicitWidth = 600;tYear.visible = true;tMonth.visible = true;tDay.visible = true;tHour.visible = true;tMinute.visible = true;tSecond.visible = true;if("Date" === type){root.implicitWidth = 420;tHour.visible = false;tMinute.visible = false;tSecond.visible = false;}if("Time" === type){root.implicitWidth = 420;tYear.visible = false;tMonth.visible = false;tDay.visible = false;}}ColumnLayout{anchors.fill: parentspacing: 0anchors.bottomMargin: 20Rectangle{Layout.fillWidth: trueLayout.preferredHeight: 30color: "#FFFFFF"Text {anchors.fill: parentverticalAlignment: Text.AlignVCenterhorizontalAlignment: Text.AlignLeftfont.pixelSize: 16color: Qt.lighter("#000000")leftPadding: 10text: root.text}}Rectangle{Layout.fillWidth: trueLayout.fillHeight: trueLayout.margins: 30radius: 6color: "#444444"RowLayout{anchors.fill: parentspacing: 0MC.Tumbler{id: tYeartext: "年"min: 2000max: 2100Layout.fillHeight: trueLayout.fillWidth: trueonCurrentChanged: recalDay()}MC.Tumbler{id: tMonthtext: "月"min: 1max: 12zero: 2Layout.fillHeight: trueLayout.fillWidth: trueonCurrentChanged: recalDay()}MC.Tumbler{id: tDaytext: "日"min: 1max: 30zero: 2Layout.fillHeight: trueLayout.fillWidth: true}MC.Tumbler{id: tHourtext: "时"min: 0max: 23zero: 2Layout.fillHeight: trueLayout.fillWidth: true}MC.Tumbler{id: tMinutetext: "分"min: 0max: 59zero: 2Layout.fillHeight: trueLayout.fillWidth: true}MC.Tumbler{id: tSecondtext: "秒"min: 0max: 59zero: 2Layout.fillHeight: trueLayout.fillWidth: true}}}RowLayout{Layout.preferredHeight: 28Layout.fillWidth: truespacing: 40Item {Layout.fillWidth: true}MC.Button{text: "保  存"Layout.preferredWidth: 80Layout.preferredHeight: 26backgroundDefaultColor: "white"contentItemTextColor: "black"onClicked: root.accept()}MC.Button{text: "取  消"Layout.preferredWidth: 80Layout.preferredHeight: 26backgroundDefaultColor: "white"contentItemTextColor: "black"onClicked: root.reject()}Item {Layout.fillWidth: true}}}function exec(value){var dateTime = new Date(value);tYear.current = dateTime.getFullYear();tMonth.current = dateTime.getMonth()+1;tDay.current = dateTime.getDate();tHour.current = dateTime.getHours();tMinute.current = dateTime.getMinutes();tSecond.current = dateTime.getSeconds();open();}//获取时间function getDateTime(){return new Date(tYear.current, tMonth.current - 1, tDay.current, tHour.current, tMinute.current, tSecond.current);}//获取时间字符 yyyy-MM-dd hh:mm:ssfunction getText(format){return Qt.formatDateTime(getDateTime(), format);}//重新计算 日function recalDay(){var year = tYear.current;var month = tMonth.current;var solarMonths = [1,3,5,7,8,10,12];var dayMax = 31;if(-1 === solarMonths.indexOf(month)){if(2 === month){if(0 === year%4)dayMax = 29;elsedayMax = 28;}elsedayMax = 30;}if(dayMax !== tDay.max)tDay.max = dayMax;}
}

Tumbler.qml

import QtQuick 2.12
import QtQuick.Layouts 1.12Rectangle {id: rootcolor: "transparent"property string text: ""property int min: -1property int max: -1property int current: -1property int zero: 0//获取最终结果function getResult(){return (myPathView.currentIndex + root.min)}ColumnLayout{anchors.fill: parentText {visible: (0 !== text.length)text: root.textfont.pixelSize: 24font.bold: truecolor: "#FFFFFF"Layout.preferredHeight: 40Layout.fillWidth: trueverticalAlignment: Text.AlignVCenterhorizontalAlignment: Text.AlignHCenter}Item {Layout.fillHeight: trueLayout.fillWidth: trueLayout.margins: 10PathView{id: myPathViewanchors.fill: parentcurrentIndex:  (-1 === root.current) ? 0 : (root.current - root.min)onCurrentIndexChanged: { root.current = currentIndex + root.min; }model: (root.max - root.min + 1)delegate:Item{//width:myPathView.width/2height:myPathView.height/myPathView.pathItemCountscale: PathView.iconScale!==undefined?PathView.iconScale:1opacity: PathView.iconOpacity!==undefined?PathView.iconOpacity:1z:PathView.iconZ!==undefined?PathView.iconZ:1transform: Rotation {origin.x: width/2origin.y: height/2axis.x: 1axis.y: 0axis.z: 0angle: PathView.iconAngle!==undefined?PathView.iconAngle:0}Text{id:timeTextanchors.centerIn: parenttext: String(Number(modelData) + root.min).padStart(root.zero, '0')color: PathView.isCurrentItem ? "#0099ff" : Qt.lighter("#FFFFFF")font.pixelSize: 30verticalAlignment: Text.AlignVCenterhorizontalAlignment: Text.AlignHCenter}}pathItemCount: 3preferredHighlightBegin: 0.5preferredHighlightEnd: 0.5highlightRangeMode: PathView.StrictlyEnforceRange//交互属性,支持拖动等……interactive: true//滑动速度maximumFlickVelocity:1000path :pathVerticalPath{//------------垂直变化------------id:pathVerticalproperty int height: myPathView.heightstartX: myPathView.width/2PathLine { id:line1; x: pathVertical.startX; y: pathVertical.startY; }PathAttribute { name: "iconZ"; value: 1 }PathAttribute { name: "iconScale"; value: 0.6 }PathAttribute { name: "iconOpacity"; value: 0.3 }PathAttribute { name: "iconAngle"; value: 80  }PathPercent { value: 0 }// start scaling upPathLine { id:line2; x: line1.x; y: line1.y + pathVertical.height; }PathAttribute { name: "iconZ"; value: 2 }PathAttribute { name: "iconScale"; value: 0.8 }PathAttribute { name: "iconOpacity"; value: 0.4 }PathAttribute { name: "iconAngle"; value: 50  }PathPercent { value: 1/4 }// middle pointPathLine { x: line2.x; y: line2.y; }PathAttribute { name: "iconZ"; value: 5 }PathAttribute { name: "iconScale"; value: 1.0 }PathAttribute { name: "iconOpacity"; value:1.0 }PathAttribute { name: "iconAngle"; value: 0  }PathPercent { value: 2/4}// start scaling downPathLine { x: line2.x; y: line2.y; }PathAttribute { name: "iconZ"; value: 2 }PathAttribute { name: "iconScale"; value: 0.8}PathAttribute { name: "iconOpacity"; value: 0.4 }PathAttribute { name: "iconAngle"; value: -50  }PathPercent { value: 3/4 }// last pointPathLine { x: line2.x; y: line2.y; }PathAttribute { name: "iconZ"; value: 1 }PathAttribute { name: "iconScale"; value: 0.6 }PathAttribute { name: "iconOpacity"; value:0.3 }PathAttribute { name: "iconAngle"; value: -80  }PathPercent { value: 1}}}Item {width: parent.widthheight:myPathView.height/myPathView.pathItemCountanchors.centerIn: parentRectangle{height: 1color: "#FFFFFF"anchors.top: parent.topanchors.left: parent.leftanchors.right: parent.right}Rectangle{height: 1color: "#FFFFFF"anchors.bottom: parent.bottomanchors.left: parent.leftanchors.right: parent.right}}}}
}

参考连接

QML 自定义控件 时间滚筒控件相关推荐

  1. QML仿手机时间滚筒控件

    QML仿手机时间滚筒控件 注意里面的图片使用自己的图片代替 使用自己封装的TimePathView.qml.TimePathItem.qml 控件 ###\效果图 DatePage ####使用代码 ...

  2. 老猪带你玩转自定义控件三——sai大神带我实现ios 8 时间滚轮控件

    ios 8 的时间滚轮控件实现了扁平化,带来很好用户体验,android没有现成控件,小弟不才,数学与算法知识不过关,顾十分苦恼,幸好在github上找到sai大神实现代码,甚为欣喜,顾把学习这个控件 ...

  3. Android开发技巧——自定义控件之组合控件

    Android开发技巧--自定义控件之组合控件 我准备在接下来一段时间,写一系列有关Android自定义控件的博客,包括如何进行各种自定义,并分享一下我所知道的其中的技巧,注意点等. 还是那句老话,尽 ...

  4. layui时间怎么设置年月日时分秒_layui-laydate时间日历控件使用方法详解

    本文实例为大家分享了laydate时间日历控件的使用方法,供大家参考,具体内容如下 此控件可使用layui或者独立版的layDate,两者初始化有些不同 在 layui 模块中使用layui.code ...

  5. 日期DatePicker与时间TimePicker控件

    在AndroidApp应用中,设置日期和时间时间也是经常遇见的,下面我们一起学习一下. 我们需要学习Android中的基本控件:(1)日期选择控件DatePicker (2)时间选择控件TimePic ...

  6. java 用户控件_C#自定义控件VS用户控件

    C#中自定义控件VS用户控件大比拼 1 自定义控件与用户控件区别 WinForm中, 用户控件(User Control):继承自 UserControl,主要用于开发 Container 控件,Co ...

  7. Android 时间显示控件 TextClock

    Android 时间显示控件 TextClock TextClock可用作显示时间,API>=17,用来替代DigitalClock. 系统设置以24小时格式的时候使用这个format andr ...

  8. 基于QT封装的音视频播放时间轴控件

    采用QT graphicsview视图框架,可以实现时间轴缩放,指针拖拉,滚动条移动,可以新增指针事件等,提供时间片添加接口. 思路:左侧车牌信息和通道列表是qwidget正常的窗口,右侧的时间轴,通 ...

  9. html小时分钟秒选择器,HTMLbootstrap时间选择器控件精确到秒 datetimepicker控件怎么精确到秒?...

    在使用 HTML 中的 bootstrap datetimepicker 选择器控件时会发现,该插件不能精确到秒钟,那么 bootstrap 时间选择器控件精确到秒能否实现呢? 其实可以进行简单的修改 ...

最新文章

  1. 前端代码标准最佳实践:CSS篇
  2. 吴恩达《Machine Learning》精炼笔记 11:推荐系统
  3. 实时获取ccd图像_四元数数控:CCD视觉检测定位系统在玻璃瓶缺陷的检测
  4. was、ihs、 mq、 db2的版本查询
  5. iphone如何使用CoreNFC
  6. 海南大学计算机调剂要求,海南大学2020年硕士研究生招生调剂的公告
  7. jquery查找父窗体id_Vue父组件获取子组件中的变量
  8. python画柱形图把奇数年份也显示出来_python - Matplotlib奇数子图 - 堆栈内存溢出...
  9. 屏幕持续升级!一加8斩获DisplayMate A+评级
  10. 域用用户怎么允许共享_w7如何共享打印机 w7共享打印机步骤【详细介绍】
  11. Server.ScriptTimeOut,Response.IsClientConnected
  12. 基于 USB 传输的针式打印机驱动程序开发
  13. 《明解c语言 入门篇》柴田望洋/著 205段代码
  14. 生活之游戏中的心理学
  15. 基于组态王6.55的电能监测系统
  16. 【python】暴力破解压缩包密码
  17. WinPE的制作 - 进WinPE后自动运行程序
  18. JUC Striped64
  19. 〖Python语法进阶篇②〗- 线程与多线程概述
  20. 当前计算机三档配置,剑灵五档配置要求 电脑配置推荐

热门文章

  1. nRF24L01+启用自动应答ACK及自动重发的实战分享
  2. AMC1100采用全差分隔离放大器的隔离式电流与电压测量
  3. 计算机网口是,百兆网口和千兆网口的区别是什么
  4. 2021年上半年健身总结
  5. 迅达cadi_迅达电梯故障代码表
  6. 数据压缩解析及C语言实现介绍
  7. Code Jam - Store Credit for Python
  8. RK3566采集显示调试--edp屏幕 imx586
  9. c语言课程设计贪吃蛇报告,贪吃蛇C语言课程设计报告.doc
  10. 网络聊天术语大全 (外二篇)