Hi, I'm trying some Qwt programming. I have made a simple program to show my problem: in the following application there are two checkboxes controlling if the respective curves are displayed and a pushbutton to get new data for the curves (here I have simulated the data with rand(), but in my real application they are retrieved from a database). The checkboxes are connected to drawCurves() which attach/detach curves to plot. Now if I click on the button the curves are displayed properly but if i toggle the checkboxes to show/hide a curve all curves gone.

Qt Code:
  1. #include <QtGui>
  2. #include <qwt_plot.h>
  3. #include <qwt_plot_curve.h>
  4. #include <cstdlib>
  5. #include <ctime>
  6.  
  7. class Dialog: public QDialog
  8. {
  9. Q_OBJECT
  10.  
  11. public:
  12. Dialog(QWidget *parent = 0);
  13.  
  14. public slots:
  15. void getData();
  16. void drawCurves();
  17.  
  18. private:
  19. QwtPlot *plot;
  20. QwtPlotCurve redCurve;
  21. QwtPlotCurve greenCurve;
  22. QCheckBox *redCurveCheckBox;
  23. QCheckBox *greenCurveCheckBox;
  24. };
  25.  
  26. Dialog::Dialog(QWidget *parent)
  27. : QDialog(parent)
  28. {
  29. srand(time(0));
  30.  
  31. redCurveCheckBox = new QCheckBox("Red Curve");
  32. redCurveCheckBox->setChecked(true);
  33. greenCurveCheckBox = new QCheckBox("Green Curve");
  34. QPushButton *drawButton = new QPushButton("Draw");
  35.  
  36. plot = new QwtPlot;
  37.  
  38. redCurve.setPen(QPen(QColor(Qt::red),2));
  39. redCurve.setStyle(QwtPlotCurve::Lines);
  40. redCurve.setRenderHint(QwtPlotItem::RenderAntialiased);
  41.  
  42. greenCurve.setPen(QPen(QColor(Qt::green),2));
  43. greenCurve.setStyle(QwtPlotCurve::Lines);
  44. greenCurve.setRenderHint(QwtPlotItem::RenderAntialiased);
  45.  
  46. getData();
  47.  
  48. QVBoxLayout *rightLayout = new QVBoxLayout;
  49. rightLayout->addWidget(redCurveCheckBox);
  50. rightLayout->addWidget(greenCurveCheckBox);
  51. rightLayout->addWidget(drawButton);
  52. rightLayout->addStretch();
  53.  
  54. QHBoxLayout *mainLayout = new QHBoxLayout;
  55. mainLayout->addWidget(plot);
  56. mainLayout->addLayout(rightLayout);
  57.  
  58. setLayout(mainLayout);
  59. resize(640,480);
  60.  
  61. connect(redCurveCheckBox, SIGNAL(clicked()), this, SLOT(drawCurves()));
  62. connect(greenCurveCheckBox, SIGNAL(clicked()), this, SLOT(drawCurves()));
  63. connect(drawButton, SIGNAL(clicked()), this, SLOT(getData()));
  64. }
  65.  
  66. void Dialog::getData()
  67. {
  68. int size = 1+rand()%10 + 10;
  69.  
  70. double redCurveData[size];
  71. double greenCurveData[size];
  72. double xval[size];
  73.  
  74. for(int i = 0; i < size; i++)
  75. {
  76. redCurveData[i] = 0+rand()%10;
  77. greenCurveData[i] = 0+rand()%10;
  78. xval[i] = i + 1;
  79. }
  80.  
  81. redCurve.setRawData(xval,redCurveData,size);
  82. greenCurve.setRawData(xval,greenCurveData,size);
  83.  
  84. plot->setAxisScale(QwtPlot::xBottom, 1, size);
  85. plot->setAxisScale(QwtPlot::yLeft, 0, 10);
  86.  
  87. drawCurves();
  88. }
  89.  
  90. void Dialog::drawCurves()
  91. {
  92. if(redCurveCheckBox->isChecked())
  93. redCurve.attach(plot);
  94. else
  95. redCurve.detach();
  96.  
  97. if(greenCurveCheckBox->isChecked())
  98. greenCurve.attach(plot);
  99. else
  100. greenCurve.detach();
  101.  
  102. plot->replot();
  103. }
  104.  
  105. int main(int argc, char *argv[])
  106. {
  107. QApplication app(argc, argv);
  108. Dialog *dialog = new Dialog;
  109. dialog->show();
  110. return app.exec();
  111. }
  112.  
  113. #include "main.moc"
To copy to clipboard, switch view to plain text mode 

Seems that the data associated to each curve is lost when un/checking: if I output the curves data during drawCurves() with this piece of code:

Qt Code:
  1. for(int i = 0; i < 9; i++)
  2. std::cout << redCurve.y(i) << " ";
  3. std::cout << std::endl;
To copy to clipboard, switch view to plain text mode 

after pushing the button I have, for example, 7 5 3 0 7 2 6 6 1, while un/checking one of the checkbox i have, for example

[HTML]3.30667e-317 -8.91495e+303 3.90058e-314 2.36344e-310 4.94066e-324 4.94066e-324 4.94066e-324 4.94066e-324 0[/HTML]

Maybe is a c++ error but I don't know where.

Regards