QHPieModelMapper crash when going from one model to another
-
Specifically the code is this:
if(dynamic_cast<pie*>(grafico)){
QHPieModelMapper* mapper = new QHPieModelMapper(this);
pie_mapper->setFirstColumn(0);
pie_mapper->setColumnCount(m->columnCount());
pie_mapper->setLabelsRow(0);
pie_mapper->setValuesRow(0);
pie_mapper->setSeries(dynamic_cast<QPieSeries*>(grafico->getSeries().at(0)));
setSeriesMapper();
pie_mapper->setModel(m);Specifically in the TableView that appears I had to put the method seriesMapper before otherwise the colors will have never shown correctly in the chart.
In seriesMapper I have one for each type of user define d chart and the snippet for Pie one is:if(dynamic_cast<pie*>(grafico)){
QList<QPieSlice > slices = dynamic_cast<QPieSeries>(grafico->getSeries().at(0))->slices();
for(int i=0; i<slices.count(); i++){
QString seriesColorHex = "#000000";
seriesColorHex = "#" + QString::number(slices.at(i)->brush().color().rgb(), 16).right(6).toUpper();
m->addMapping(seriesColorHex, QRect(i, 0, 1, m->columnCount()));
}
}
lYou can see how the UI looks hereWhenever I go from one chart to another, Qt crashes, the debugger tells is QHPieModelMapperPrivate::slices_removed.
I don't know how to solve this problem, it's driving me insane.It seems there's a misguidance between the mapper and the series, commenting setModel or setSeries makes it not crash but of course not mapping the model and making the changes to the chart.
The whole code is this:
void TableWidget::creaGrafico(){
m = new Mapper(grafico);
tableView = new QTableView();
tableView->setModel(m);
tableView->setMinimumWidth(300);
tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
m->setParent(tableView);
if(dynamic_cast<line*>(grafico)){
int cont = 0;
for(int i=0; i<grafico->getSeries().size(); i++){
QVXYModelMapper mapper = new QVXYModelMapper(this);
mapper->setXColumn(cont);
cont++;
mapper->setYColumn(cont);
cont++;
mapper->setSeries(dynamic_cast<QLineSeries>(grafico->getSeries().at(i)));
mapper->setModel(m);
setSeriesMapper();
}
}
if(dynamic_cast<CandleStick*>(grafico)){
int cont = 0;
QVCandlestickModelMapper mapper = new QVCandlestickModelMapper(this);
mapper->setFirstSetColumn(0);
mapper->setLastSetColumn(m->columnCount()-1);
mapper->setTimestampRow(cont);
cont++;
mapper->setOpenRow(cont);
cont++;
mapper->setHighRow(cont);
cont++;
mapper->setLowRow(cont);
cont++;
mapper->setCloseRow(cont);
cont++;
mapper->setSeries(dynamic_cast<QCandlestickSeries>(grafico->getSeries().at(0)));
mapper->setModel(m);
}
if(dynamic_cast<pie*>(grafico)){
pie_mapper->setFirstColumn(0);
pie_mapper->setColumnCount(m->columnCount());
pie_mapper->setLabelsRow(0);
pie_mapper->setValuesRow(0);
pie_mapper->setSeries(dynamic_cast<QPieSeries*>(grafico->getSeries().at(0)));
setSeriesMapper();
pie_mapper->setModel(m);
}
if(dynamic_cast<bar*>(grafico)){
QVBarModelMapper mapper = new QVBarModelMapper(this);
mapper->setFirstBarSetColumn(0);
mapper->setLastBarSetColumn(m->columnCount()-1);
mapper->setSeries(dynamic_cast<QBarSeries>(grafico->getSeries().at(0)));
mapper->setModel(m);
setSeriesMapper();
}
chartView = new QChartView(grafico->getGrafico());
chartView->setRenderHint(QPainter::Antialiasing);
chartView->setMinimumSize(640, 480);
}The MainWIndow code for pie creation is this:
void MainWindow::on_pie_c_clicked()
{
c->clean();
c = new pie("ciao");
t = new TableWidget(c);
c->insert("a", {"20"});
c->insert("b", {"40"});
c->insert("c", {"10"});
t->creaGrafico();mainLayout = new QGridLayout; createMenuBar(); createChartMenu(); createWidgets(); createEditColMenu(); createOptions(); QWidget* mainWidget = new QWidget(); mainWidget->setLayout(mainLayout); setCentralWidget(mainWidget);
}
And the Pie user defined class makes use of this methods in other to show and work (I created custom axis for the chart, but also on the others and the problem is only Pie)
void pie::insert(QString x, QStringList y){
labels.append(x);
values.append(y);
series->append(x, y[0].toDouble());
finalizza();
}void pie::finalizza(){
serie.push_back(series);
if(!grafico->series().isEmpty()){
grafico->removeSeries(series);
}
grafico->addSeries(series);
if(maxY > 0 && !axisX->categories().isEmpty()){
for(int i=0; i<serie.size(); i++){
QList<QAbstractAxis*> a = serie[i]->attachedAxes();
if(a.isEmpty()){
serie[i]->attachAxis(axisX);
serie[i]->attachAxis(axisY);
}
else{
serie[i]->detachAxis(axisX);
serie[i]->detachAxis(axisY);
serie[i]->attachAxis(axisX);
serie[i]->attachAxis(axisY);
}
}
}
}
The clean method, cleans my user defined data, which are two QStringList, labels and values, used to display and edit data between the charts, so it's simply:void Chart::clean(){
values.clear();
labels.clear();
}Given that Qt gives BarModelMapper and LineMOdelMapper and no documentation on anything, especially this, I hope you can solve this. I can't take it anymore.
Thank you if anything useful comes out of this. -
Seems like the problem was only for Pie, cause Candlestick, Bar and Line had no problem.
In Mainwindow the declaration was for:QGridLayout mainLayout=new QGridLayout();
and redefining for the PieSeries isn't a good thing.
Thank you QT Charts for being totally poor in explanation in everything and the debugger was telling something completely different from the actual thing.So deleting mainLayout = new QGridLayout;
as a line solves the problem.For the poor people who desperately won't find a solution, this was it. The layout.