error: ‘class QGraphicsView’ has no member named ‘setChart’
时间: 2023-08-26 10:57:34 浏览: 562
抱歉,我之前的回答有误。`QGraphicsView`确实没有`setChart`方法,因为它是一个用于显示图形项的视图类,而不是用于显示图表的类。如果你想在`QGraphicsView`中显示饼状图,你需要使用`QChartView`作为它的子类。
请将你的代码中的`QGraphicsView`改为`QChartView`,然后再尝试运行。修改后的代码如下所示:
```cpp
QPieSeries *pie_series;
pie_series = new QPieSeries(this);
//定义各扇形切片的颜色
static const QStringList list_pie_color = {
"#6480D6","#A1DC85","#FFAD25","#FF7777","#84D1EF","#4CB383",
};
//设置数据
QList<qreal> list_data = {3.1, 3.2, 3.3, 3.4, 3.5, 3.6};
//扇形
for (int i = 0; i < list_pie_color.size(); i++)
{
QPieSlice* pie_slice = new QPieSlice(this);
pie_slice->setLabelVisible(true);
pie_slice->setValue(list_data[i]);
pie_slice->setLabel(QString::number(list_data[i]));
pie_slice->setColor(list_pie_color[i]);
pie_slice->setLabelColor(list_pie_color[i]);
pie_slice->setBorderColor(list_pie_color[i]);
pie_series->append(pie_slice);
}
//图表视图
QChart* chart = new QChart;
chart->setTitle("岩性分布统计图");
// chart->setTheme(QChart::ChartThemeDark);
//标题字体
QFont font = qApp->font();
font.setBold(true);
font.setPointSize(16);
chart->setTitleFont(font);
//加入饼图
chart->addSeries(pie_series);
chart->setAnimationOptions(QChart::SeriesAnimations);
//图例
chart->legend()->setAlignment(Qt::AlignBottom);
chart->legend()->setBackgroundVisible(false);
QChartView* chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
chartView->setFixedSize(400, 400);
ui->graphicsView_type_pie->setScene(new QGraphicsScene());
ui->graphicsView_type_pie->scene()->addWidget(chartView);
```
这样修改后,你应该能够在`QGraphicsView`中显示饼状图了。
阅读全文