# QT学习 之 三维饼图绘制

29 篇文章 6 订阅

QT里没有相应统计图形的绘制组件

void aaq::paintEvent( QPaintEvent* ev )
{
QPainter painter(this);
// 去除画笔
painter.setPen(Qt::NoPen);
// 设置反锯齿
painter.setRenderHint(QPainter::Antialiasing);

//******************************************** 圆饼图 *****************************************//
if(1)
{
// 顶层圆面
QRectF rect_top(10.0, 20.0, 280.0, 160.0);
// 底层圆面
QRectF rect_bottom(10.0, 60.0, 280.0, 160.0);
// 中间矩形
QRectF rect_midd(10.0, 100.0, 280.0, 40.0);

// 扇形起始角度
int startAngle = 230 * 16;
// 扇形覆盖范围
int spanAngle = 60 * 16;

painter.setBrush(QColor(97,35,35,255));
// 绘制底层圆面
painter.drawEllipse(rect_bottom);
// 绘制中间矩形
painter.drawRect(rect_midd);

painter.setBrush(QColor(Qt::darkYellow));
// 绘制底层扇形
painter.drawPie(rect_bottom, startAngle, spanAngle);

// 扇形的弦与弧的交点
double pi = 3.1415926;
double dx1 = rect_top.width() * 0.5 * cos(230 * pi / 180);
double dy1 = rect_top.height() * 0.5 * sin(230 * pi / 180);

double dx2 = rect_top.width() * 0.5 * cos(290 * pi / 180);
double dy2 = rect_top.height() * 0.5 * sin(290 * pi / 180);

// 求交点的坐标值
QPointF posBackCenter = QPointF(rect_top.center());
double dX1 = posBackCenter.x() + dx1 + 0.5;
double dY1 = posBackCenter.y() - dy1 + 0.5;

double dX2 = posBackCenter.x() + dx2 + 0.5;
double dY2 = posBackCenter.y() - dy2 + 0.5;

// 记录交点
QPointF topLeft = QPointF(dX1, dY1);
QPointF bottomRight = QPointF(dX2, dY2) + QPointF(0,40);
QPointF bottomLeft = topLeft + QPointF(0,40);
painter.setBrush(QColor(Qt::darkYellow));

// 绘制连接扇形的区域
QPolygonF path;
path << topLeft << QPointF(dX2,dY2) << bottomRight << bottomLeft;
painter.drawPolygon(path);

// 绘制顶层圆面
painter.setBrush(QColor(156,56,56,255));
painter.drawEllipse(rect_top);

// 绘制顶层扇形
painter.setBrush(QColor(Qt::yellow));
painter.drawPie(rect_top, startAngle, spanAngle);
}

QWidget::paintEvent(ev);
}
• 1
点赞
• 8
收藏
觉得还不错? 一键收藏
• 3
评论
01-31 681
12-26 751
11-08 6724
06-05 934
10-18 1万+
07-24 1345
01-07
07-14
03-24
08-22
11-05
10-30
06-11 3万+
11-26 2万+

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。