如何在Qt或Qml中向模拟时钟添加滴答声? 以下是我的时钟代码。
Canvas
{
id : canvas
anchors.fill: parent
width: 740; height: 680 //size of canvas
onPaint: // reimplement drawing function
{
var ctx = getContext("2d"); //get 2D Context
drawBack(ctx);
drawDot(ctx);
var date = new Date(); //get date
var hours = date.getHours() //get hour
hours = hours%12;
var minutes = date.getMinutes()
var seconds = date.getUTCSeconds();
var timestr =date.toLocaleString("yyyy-MM-dd hh:mm:ss");
console.log(date.toLocaleString("yyyy-MM-dd hh:mm:ss"));
drawSecond(seconds,ctx);
drawHour(hours,minutes,ctx);
drawMinute(minutes,seconds,ctx);
drawSecond(seconds,ctx);
}
}
function drawBack(ctx)
{
var r = 200;
ctx.save();
ctx.beginPath();
ctx.translate(width/2,height/2);
ctx.clearRect(-r,-r,r*2,r*2);
ctx.lineWidth = 10;
ctx.arc(0,0,r-5,0,Math.PI*2,false); //outer circle
ctx.stroke();
var numbers = [1,2,3,4,5,6,7,8,9,10,11,12]; //numbers in clock
ctx.font = "30px Arial"; //set font
ctx.textAlign = "center";
ctx.textBaseline = "middle";
for(var i = 0; i < 12; ++i)
{
var rad = 2*Math.PI/12*numbers[i]-3.14/2;
var x = Math.cos(rad)*(r-30);
var y = Math.sin(rad)*(r-30);
ctx.fillText(numbers[i],x,y);
}
ctx.lineWidth = 2; // for scales
for (var i2 = 0; i2 <60; ++i2)
{
ctx.beginPath();
var rad2 = 2*Math.PI/60*i;
var x3 = Math.cos(rad)*(r-15);
var y3 = Math.sin(rad)*(r-15);
var x2 = Math.cos(rad)*(r-10);
var y2 = Math.sin(rad)*(r-10);
}
ctx.restore();
}
function drawHour(hour, minute, ctx) //draw hours
{
var r = 200;
ctx.save(); // save the state
ctx.beginPath(); // this line is necessary
ctx.lineWidth = 7;
ctx.lineCap = "round"; //set the style
ctx.translate(width/2,height/2);
var rad = Math.PI*2/12*hour+Math.PI*2*minute/12/60-Math.PI;
ctx.rotate(rad);
ctx.moveTo(0,-10);
ctx.lineTo(0,r/2-10);
ctx.stroke(); // fill or stroke is necessary
ctx.restore();
}
function drawMinute(minute,second,ctx) //draw Minute
{
var r = 200;
ctx.save();
ctx.beginPath(); //necessary
ctx.lineWidth = 5;
ctx.lineCap = "round";
ctx.translate(width/2,height/2);
var rad = Math.PI*2*minute/60 + Math.PI*2*second/60/60 -Math.PI;
ctx.rotate(rad);
ctx.moveTo(0,-10);
ctx.lineTo(-10,r*3/5-10);
ctx.stroke(); //like pen in Qt
ctx.restore();
}
function drawSecond(second,ctx) //draw Second
{
var r = 200;
ctx.save();
ctx.beginPath();
ctx.lineWidth = 3;
ctx.lineCap = "round";
ctx.translate(width/2,height/2);
ctx.strokeStyle="#ff0000";
var rad = Math.PI*2*second/60-Math.PI;
ctx.rotate(rad);
ctx.moveTo(0,-20);
ctx.lineTo(0,r*4/5-10);
ctx.stroke();
ctx.restore();
}
function drawDot(ctx)
{
ctx.save();
ctx.beginPath();
ctx.translate(width/2,height/2);
ctx.fillStyle="#ffffff";
ctx.arc(0,0,3,0,Math.PI*2);
ctx.fill(); //like a brush
ctx.restore();
}
Timer {
interval: 1000; running: true; repeat: true
onTriggered: canvas.requestPaint(); //trigger Timer
}
}
答案 0 :(得分:1)
您可以使用带有滴答声和SoundEffect的wav文件。您需要在QML文件中导入QtMultimedia。
SoundEffect {
id: playTick
source: "tick.wav"
}
Timer {
interval: 1000; running: true; repeat: true
onTriggered: {
canvas.requestPaint()
playTick.play()
}
}