ui->widget->setStyleSheet("QWidget{background-color:#222648;}"); series = new QPieSeries(); series->append("20%", 2); series->append("50%", 5); series->append("30%", 3); series->setLabelsVisible(); QPieSlice *slice_red = series->slices().at(0); //添加扇區 QPieSlice *slice_green = series->slices().at(1); QPieSlice *slice_black = series->slices().at(2); slice_red->setColor(QColor(199,38,22)); //設置顏色 slice_green->setColor(QColor(0,165,94)); slice_black->setColor(QColor(54,54,54)); slice_red->setLabelColor(QColor(255,255,255)); //設置label的顏色 slice_green->setLabelColor(QColor(255,255,255)); slice_black->setLabelColor(QColor(255,255,255)); //slice_red->setExploded();//展開 QFont font; font.setPointSize(9); //設置字體大小,如果字體過大容易顯示不出來 slice_red->setLabelFont(font); slice_green->setLabelFont(font); slice_black->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());
在使用時,需要在pro文件里面添加一行代碼
QT += charts