QWidget显示饼图

一、环境准备

1、安装Qt的时候需要勾上chart

2、.pro

QT  += charts

3、添加头文件

#include <QtChar

二、在ui里拖一个widget过去

 

 

三、代码

1、mainwindow.h声明一个饼图

QPieSeries *series;//饼图相关

2、mainwindow.cpp实现

   ui->widget->setStyleSheet("QWidget{background-color:#222648;}");
    series = new QPieSeries();
    series->append("20%", 2);
    series->append("80%", 8);
    series->setLabelsVisible();

    QPieSlice *slice_red = series->slices().at(0);
    QPieSlice *slice_green = series->slices().at(1);
    slice_red->setColor(QColor(199,38,22));
    slice_green->setColor(QColor(0,165,94));
    slice_red->setLabelColor(QColor(255,255,255));
    slice_green->setLabelColor(QColor(255,255,255));
    //slice_red->setExploded();//展开
    QFont font;
    font.setPointSize(12);
    slice_red->setLabelFont(font);
    slice_green->setLabelFont(font);

    QChart *chart = new QChart();
    chart->addSeries(series);
    chart->legend()->hide();
    chart->setBackgroundBrush(QBrush(QColor(34,38,72)));
    chart->setAnimationOptions(QChart::AllAnimations);//设置启用或禁用动画

    QChartView *chartview = new QChartView(chart);
    chartview->setRenderHint(QPainter::Antialiasing);
    chartview->setParent(ui->widget);
    chartview->resize(ui->widget->size());

  

 

posted @ 2020-07-29 17:07  朱小勇  阅读(579)  评论(0编辑  收藏  举报