Hi, I want to show a array of graphs in a QCustomplot object but failed to figure out how to properly arrange the layout system. Please help.
Details: Say I have a array of 10 graphs, and want to show them in a 2x2 grid, each cell of the grid only show one element of the array. So for each time, only 4 of the graphs will be shown.
I will allow users to choose which 4 graphs to show, and the data of all 10 graphs are keep coming in from external part (of the same program actually).
My current 'logical' attempt look like this:
ctor(MyChartWidget): create sublayout with 4 QAxisRect + 4 QGraphs, and set the layers init(MyChartWidget): prepare data holders for those 10 graphs, call graphSelect() for initial 4 graphs graphSelect(MyChartWidget): assign 4 graphdata from the 10 dataholders and call replot()
Problems: The program crash whenever the second call of 'replot()'. Is that anything I missed for the layout?
MultiChartWidget::MultiChartWidget(QWidget *parent) : QCustomPlot(parent) { max_checktable_ = 4; // let's start from scratch and remove the default axis rect plotLayout()->clear(); // create a sub layout that we'll place in first row: QCPLayoutGrid *subLayout = new QCPLayoutGrid(); plotLayout()->addElement(0, 0, subLayout); QList< QCPAxisRect* > axis_rect_; for (int i = 0; i < max_checktable_; i++) { QCPAxisRect* r = new QCPAxisRect(this); axis_rect_.push_back(r); r->setLayer("background"); foreach (QCPAxis *axis, r->axes()) { axis->setLayer("axes"); axis->grid()->setLayer("grid"); } QCPLegend* l = new QCPLegend; sub_legend_.push_back(l); l->setLayer("legend"); l->setVisible(false); r->insetLayout()->addElement(l, Qt::AlignRight|Qt::AlignTop); r->insetLayout()->setMargins(QMargins(12, 12, 12, 12)); QCPGraph* g = addGraph(r->axis(QCPAxis::atBottom), r->axis(QCPAxis::atLeft)); sub_graphs_.push_back(g); } subLayout->addElement(0, 0, axis_rect_[0]); subLayout->addElement(0, 1, axis_rect_[1]); subLayout->addElement(1, 0, axis_rect_[2]); subLayout->addElement(1, 1, axis_rect_[3]); // setChartTitle("Cumulative District-specific Attack Rate"); } // end_ctor(MultiChartWidget)
void MultiChartWidget::init(QStringList graphNames, int defaultSelected) { graph_names_.swap(graphNames); graph_names_.insert(0, "Overall"); for (int i = 0; i < graph_data_.size(); i++) { graph_data_[i]->clear(); delete graph_data_[i]; } graph_data_.clear(); for (int i = 0; i < graph_names_.size(); i++) { graph_data_.push_back(new QCPDataMap()); selected_indices_.push_back(false); } selectGraphs(QList<int>() << defaultSelected); } // end: MultiChartWidget::init()
void MultiChartWidget::selectGraphs(QList<int> indices) { for (int i = 0; i < graph_names_.size(); i++) selected_indices_[i] = false; for (int i = 0; i < indices.size(); i++) { int selected = indices[i]; selected_indices_[ selected ] = true; sub_graphs_[i]->setName( graph_names_[ selected ] ); sub_graphs_[i]->setData( graph_data_[ selected ] ); } qDebug() << "MultiChartWidget-selectGraphs-pre-replot"; replot(); // crashes every 2nd call of this qDebug() << "MultiChartWidget-end-selectGraphs"; } // end: MultiChartWidget::setSelectedGraphs()