我刚刚开始qt并尝试制作饼图,并且我有以下代码
QPainter painter(this);
QPen pen;
QRectF size;
pen.setColor(Qt::white);
pen.setWidth(0);
painter.setPen(pen);
if(this->height()>this->width()){
size = QRectF(5,5,this->width()-10, this->width()-10);
}
else{
size = QRectF(5,5,this->height()-10, this->height()-10);
}
double sum = 0.0, startAngle = 0.0;
double angle = 0.0, endAngle, percent;
for(auto i = 0; i < qvValues.size(); i++){
sum += qvValues[i];
}
for(auto i = 0; i < qvValues.size(); i++){
percent = qvValues[i] / sum;
angle = percent *360.0;
endAngle = startAngle + angle;
painter.setBrush(qvColors[i]);
painter.drawPie(size, startAngle*16, angle*16);
painter.drawText(size,"text");
startAngle = endAngle;
}
正在得到以下结果
饼图
我的问题是,如何能够使用QPainter类中的drawText()方法在每个饼图中添加文本?具体来说,我需要获取切片的坐标并将其传递到drawText()吗?如果是这样,您将如何访问其x和y?