QCustomPlot Discussion and Comments

Possible bug in Interaction Example: void MainWindow::selectionChanged()Return to overview

This code block from Interaction Example: void MainWindow::selectionChanged() randomly crashes my program because QCPPlottableLegendItem *item is QObject() instead of QCPPlottableLegendItem()

  
// synchronize selection of graphs with selection of corresponding legend items:
  for (int i=0; i<ui->customPlot->graphCount(); ++i)
  {
    QCPGraph *graph = ui->customPlot->graph(i);
    QCPPlottableLegendItem *item = ui->customPlot->legend->itemWithPlottable(graph);
    if (item->selected() || graph->selected())
    {
      item->setSelected(true);
      graph->setSelection(QCPDataSelection(graph->data()->dataRange()));
    }
  }
}

I "fixed" and found the problem with this check:

...
qWarning() << i << w->graph(i) << item;
if (item) {
              if (item->selected() || graph->selected())
              {
                item->setSelected(true);
                graph->setSelection(QCPDataSelection(graph->data()->dataRange()));
              }
          }
          if (!item) {
              qWarning() << "no item! Is this qcustomplot bug?";
          }
...

Here are the warnings I get sometimes:
Warning: 0 QCPGraph(0x251b6c3d6e0) QObject(0x0)
Warning: no item! Is this qcustomplot bug?
Warning: 1 QCPGraph(0x251b6c3cfc0) QObject(0x0)
Warning: no item! Is this qcustomplot bug?
Warning: 2 QCPGraph(0x251b6c3c640) QObject(0x0)
Warning: no item! Is this qcustomplot bug?
Warning: 3 QCPGraph(0x251b6fb19d0) QCPPlottableLegendItem(0x251b7097960)
Warning: 4 QCPGraph(0x251b6fb1fc0) QCPPlottableLegendItem(0x251b6fef940)
Warning: 5 QCPGraph(0x251b6fb26e0) QCPPlottableLegendItem(0x251b6ff11b0)

After some thinking and testing.. I do not think this a bug.

I only get it when my program is running and replotting. Every replot changes the name of the graph also, so this is completely different scenario than in Interaction Example.