一、效果展示
效果如图1所示,时钟列表支持鼠标左右拖动,带有黑色背景的是晚上时钟,无黑色背景的是白天时钟
二、源码分析
1、main.cpp文件中只包含了一个宏,该宏的具体解释请看qml 示例中的关键宏文章
2、时钟项
点击(此处)折叠或打开
-
Item {
-
id : clock
-
width: {
-
if (ListView.view && ListView.view.width >= 200)
-
return ListView.view.width / Math.floor(ListView.view.width / 200.0);
-
else
-
return 200;
-
}
-
-
height: {
-
if (ListView.view && ListView.view.height >= 240)
-
return ListView.view.height;
-
else
-
return 240;
-
}
-
-
property alias city: cityLabel.text//属性别名,方便在外部使用
-
property int hours//自定义属性
-
property int minutes
-
property int seconds
-
property real shift
-
property bool night: false//是否是晚上 来决定是否显示黑色实心圈
-
property bool internationalTime: true //Unset for local time
-
-
//js函数
-
function timeChanged() {
-
var date = new Date;
-
hours = internationalTime ? date.getUTCHours() + Math.floor(clock.shift) : date.getHours()
-
night = ( hours < 7 || hours > 19 )
-
minutes = internationalTime ? date.getUTCMinutes() + ((clock.shift % 1) * 60) : date.getMinutes()
-
seconds = date.getUTCSeconds();
-
}
-
-
Timer {
-
interval: 100; running: true; repeat: true;//一个每隔100ms重复执行的定时器,默认启动
-
onTriggered: clock.timeChanged()//定时器槽函数
-
}
-
-
Item {
-
anchors.centerIn: parent
-
width: 200; height: 240
-
-
Image { id: background; source: "clock.png"; visible: clock.night == false }//最外层白色圈
-
Image { source: "clock-night.png"; visible: clock.night == true }//黑色实心圈,带有白色实心圆球的刻度点
-
-
//时针
-
Image {
-
x: 92.5; y: 27
-
source: "hour.png"
-
transform: Rotation {
-
id: hourRotation
-
origin.x: 7.5; origin.y: 73;
-
angle: (clock.hours * 30) + (clock.minutes * 0.5)
-
Behavior on angle {
-
SpringAnimation { spring: 2; damping: 0.2; modulus: 360 }
-
}
-
}
-
}
-
-
//分针
-
Image {
-
x: 93.5; y: 17
-
source: "minute.png"
-
transform: Rotation {
-
id: minuteRotation
-
origin.x: 6.5; origin.y: 83;
-
angle: clock.minutes * 6
-
Behavior on angle {
-
SpringAnimation { spring: 2; damping: 0.2; modulus: 360 }
-
}
-
}
-
}
-
-
//秒针
-
Image {
-
x: 97.5; y: 20
-
source: "second.png"
-
transform: Rotation {
-
id: secondRotation
-
origin.x: 2.5; origin.y: 80;
-
angle: clock.seconds * 6
-
Behavior on angle {
-
SpringAnimation { spring: 2; damping: 0.2; modulus: 360 }
-
}
-
}
-
}
-
//中心白色圆圈
-
Image {
-
anchors.centerIn: background; source: "center.png"
-
}
-
-
Text {
-
id: cityLabel//该属性已经被导出,在clocks.qml文件中指定该属性值
-
y: 210; anchors.horizontalCenter: parent.horizontalCenter
-
color: "white"
-
font.family: "Helvetica"
-
font.bold: true; font.pixelSize: 16
-
style: Text.Raised; styleColor: "black"
-
}
-
}
- }
3、时钟列表
点击(此处)折叠或打开
-
import "content" as Content //导入目录 该目录底下的所有qml自定义控件均可以直接使用
-
-
Rectangle {
-
id: root
-
width: 640; height: 320
-
color: "#646464"
-
-
ListView {//列表视图
-
id: clockview//唯一id
-
anchors.fill: parent//填充整个父窗口
-
orientation: ListView.Horizontal//列表朝向为水平方向
-
cacheBuffer: 2000//左右2000个像素以内的委托项都不会被析构
-
snapMode: ListView.SnapOneItem//拖拽模式
-
highlightRangeMode: ListView.ApplyRange//高亮模式,高亮尽可能在可是区间内
-
-
delegate: Content.Clock { city: cityName; shift: timeShift }//设置Clock控件的导出属性值
-
model: ListModel {//静态model
-
ListElement { cityName: "New York"; timeShift: -4 }
-
ListElement { cityName: "London"; timeShift: 0 }
-
ListElement { cityName: "Oslo"; timeShift: 1 }
-
ListElement { cityName: "Mumbai"; timeShift: 5.5 }
-
ListElement { cityName: "Tokyo"; timeShift: 9 }
-
ListElement { cityName: "Brisbane"; timeShift: 10 }
-
ListElement { cityName: "Los Angeles"; timeShift: -8 }
-
}
-
}
-
//左下角上一个箭头
-
Image {
-
anchors.left: parent.left
-
anchors.bottom: parent.bottom
-
anchors.margins: 10
-
source: "content/arrow.png"
-
rotation: -90
-
opacity: clockview.atXBeginning ? 0 : 0.5//当视图滚动到第一个时透明度为0(使用行为动画)
-
Behavior on opacity { NumberAnimation { duration: 500 } }//在透明度属性上添加动画(数字动画)
-
}
-
//右下角下一个箭头
-
Image {
-
anchors.right: parent.right
-
anchors.bottom: parent.bottom
-
anchors.margins: 10
-
source: "content/arrow.png"
-
rotation: 90
-
opacity: clockview.atXEnd ? 0 : 0.5
-
Behavior on opacity { NumberAnimation { duration: 500 } }
-
}
- }