是,使用Canvas(和Context2D):
import QtQuick 2.3
Rectangle {
width: 400
height: 400
Canvas {
anchors.fill: parent
onPaint: {
var ctx = getContext("2d");
ctx.reset();
var centreX = width/2;
var centreY = height/2;
ctx.beginPath();
ctx.fillStyle = "black";
ctx.moveTo(centreX, centreY);
ctx.arc(centreX, centreY, width/4, 0, Math.PI * 0.5, false);
ctx.lineTo(centreX, centreY);
ctx.fill();
ctx.beginPath();
ctx.fillStyle = "red";
ctx.moveTo(centreX, centreY);
ctx.arc(centreX, centreY, width/4, Math.PI * 0.5, Math.PI * 2, false);
ctx.lineTo(centreX, centreY);
ctx.fill();
}
}
}
我居然把这个代码从this答案,因为Qt的帆布实现HTML5的Canvas API。这使得在网络上很容易找到示例;例如,只需搜索“绘制饼图切片html5画布”。
对于鼠标的检测,你必须刷过你的数学技能...
...或只是从here窃取代码。 :)
请注意,Canvas仅在调整大小时或在调用requestPaint()时重绘,因此如果要根据鼠标位置更改切片的颜色,则需要调用该函数以查看颜色更改。