Qt图表条形图调整大小崩溃

时间:2018-10-18 07:41:34

标签: c++ qt plot bar-chart qtchart

情况:

我使用QtChartsQChartViewQChartQBarSeriesQBarSetQValueAxisQCategoryAxis编写了条形图而且效果很好。

问题:

当我调整窗口大小或在绘图上使用QRubberband时,它会崩溃。

我尝试过的事情:

我写的所有其他情节都不受该问题影响,包括使用QStackedBarSeries的情节。该问题也发生在我在相同情况下编写的另一个程序中。

代码-头:

int D_Plot::Plot_Stat_Multi_MeanMedian(
QChartView *pChartView, 
vector<vector<double> > *vv_SetSta, 
bool pl_mean, 
bool pl_sd, 
bool pl_median, 
bool pl_adm, 
QString name_title, 
QString name_categories, 
QString name_y)

代码-正文:

//Chart
QChart *chart = new QChart();
chart->setTitle(name_title);

//Sets
QBarSet set_mean("Mean");
QBarSet set_sdev("Standard Deviation");
QBarSet set_medi("Median");
QBarSet set_aadm("Average Absolute Deviation from Median");

//Categories
QStringList categories;

//Series
QBarSeries *series = new QBarSeries();
for(unsigned int set = 0; set < vv_SetSta->size(); set++)
{
    if(pl_mean)     set_mean.append((*vv_SetSta)[set][c_STAT_MEAN_ARITMETIC]);
    if(pl_sd)       set_sdev.append((*vv_SetSta)[set][c_STAT_STAN_DEV_SAMPLE]);
    if(pl_median)   set_medi.append((*vv_SetSta)[set][c_STAT_MEDIAN]);
    if(pl_adm)      set_aadm.append((*vv_SetSta)[set][c_STAT_ABS_DEV_MED]);

    categories.append(QString::number(set));
}
if(pl_mean)     series->append(&set_mean);
if(pl_sd)       series->append(&set_sdev);
if(pl_median)   series->append(&set_medi);
if(pl_adm)      series->append(&set_aadm);
chart->addSeries(series);

//Axis
QBarCategoryAxis *X_axis = new QBarCategoryAxis();
X_axis->append(categories);
X_axis->setTitleText(name_categories);
chart->setAxisX(X_axis, series);

QValueAxis *Y_axis = new QValueAxis();
Y_axis->setTitleText(name_y);
chart->setAxisY(Y_axis, series);


//Showing
pChartView->setChart(chart);

return ER_Okay;

1 个答案:

答案 0 :(得分:2)

您正在本地创建QBarSet对象(即,不通过new运算符),并将指向这些集合的指针传递给QBarSet :: append方法,如果所指向的对象(即当有义务取消分配对象时),则AFAIK将获得所有权超出范围。对于本地分配的对象,这势必会失败。