Results 1 to 10 of 10

Thread: is it a C++ error or Qt/Qwt related?

  1. #1
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default is it a C++ error or Qt/Qwt related?

    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
    Giuseppe CalÃ

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: is it a C++ error or Qt/Qwt related?

    Qwt docs say:
    setRawData is provided for efficiency. It is important to keep the pointers during the lifetime of the underlying QwtCPointerData class.
    but you pass pointers to local variables to every call to setRawData().

  3. #3
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: is it a C++ error or Qt/Qwt related?

    Sorry if I ask, but how should I modify the code?

    Thanks
    Giuseppe CalÃ

  4. #4
    Join Date
    Jan 2006
    Posts
    31
    Thanked 5 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: is it a C++ error or Qt/Qwt related?

    Allocate data on the heap.

    Put somewhere in your private section:

    Qt Code:
    1. QVector<double*> samplesPtr;
    2. int samplesSize;
    To copy to clipboard, switch view to plain text mode 

    Then modify as follows

    Qt Code:
    1. void Dialog::getData()
    2. {
    3.  
    4. samplesSize = 1+rand()%10 + 10;
    5.  
    6. double* redCurveDataPtr = new double[samplesSize];
    7. double* greenCurveData = new double[samplesSize];
    8. double* xval = new double[samplesSize];
    9.  
    10. samplesPtr << redCurveDataPtr << greenCurveData << xval;
    11.  
    12. .......
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 

    Of course you need to add a Dialog::freeData() to release the memory when no more needed. That's the reason you may want to set samplesPtr and samplesSize as class memeber data.

  5. The following user says thank you to mcostalba for this useful post:

    jiveaxe (17th November 2007)

  6. #5
    Join Date
    Jan 2006
    Posts
    31
    Thanked 5 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: is it a C++ error or Qt/Qwt related?

    Interestin enough delete[] works also from copied pointers, it means that you don't need

    int samplesSize;

    as member data but a local variable will suffice and freeData() became:

    Qt Code:
    1. void Dialog::freeData()
    2. {
    3. for(int i = 0; i < samplesPtr.size(); i++)
    4. delete[] samplesPtr.at(i);
    5. }
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to mcostalba for this useful post:

    jiveaxe (17th November 2007)

  8. #6
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: is it a C++ error or Qt/Qwt related?

    Thank you, mcostalba, your code works good.

    Regards
    Giuseppe CalÃ

  9. #7
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: is it a C++ error or Qt/Qwt related?

    One more question: the freeData() function must be called in the destructor only or before each getData()?

    Thanks
    Giuseppe CalÃ

  10. #8
    Join Date
    Jan 2006
    Posts
    31
    Thanked 5 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: is it a C++ error or Qt/Qwt related?

    After

    Qt Code:
    1. redCurve.setRawData(xval,redCurveData,size);
    2. greenCurve.setRawData(xval,greenCurveData,size);
    To copy to clipboard, switch view to plain text mode 

    you don't need the previous samples anymore (it seems) so you can call freeData() after that, of course delaying assign of new pointers until after the free of old data:

    Qt Code:
    1. void Dialog::getData()
    2. {
    3. int size = 1+rand()%10 + 10;
    4.  
    5. double* redCurveDataPtr = new double[size];
    6. double* greenCurveData = new double[size];
    7. double* xval = new double[size];
    8.  
    9. for(int i = 0; i < size; i++)
    10. {
    11. redCurveData[i] = 0+rand()%10;
    12. greenCurveData[i] = 0+rand()%10;
    13. xval[i] = i + 1;
    14. }
    15.  
    16. redCurve.setRawData(xval,redCurveData,size);
    17. greenCurve.setRawData(xval,greenCurveData,size);
    18.  
    19. // old data is no more needed now
    20. freeData();
    21.  
    22. // now we can add newly created samples pointers
    23. samplesPtr << redCurveDataPtr << greenCurveData << xval;
    24.  
    25. plot->setAxisScale(QwtPlot::xBottom, 1, size);
    26. plot->setAxisScale(QwtPlot::yLeft, 0, 10);
    27.  
    28. drawCurves();
    29. }
    To copy to clipboard, switch view to plain text mode 

  11. #9
    Join Date
    Jan 2006
    Posts
    31
    Thanked 5 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: is it a C++ error or Qt/Qwt related?

    A couple of bugs

    Because pointer vector is reused freeData() should be:

    Qt Code:
    1. void Dialog::freeData()
    2. {
    3. for(int i = 0; i < samplesPtr.size(); i++)
    4. delete[] samplesPtr.at(i);
    5.  
    6. samplesPtr.clear();
    7. }
    To copy to clipboard, switch view to plain text mode 

    And also remember to add a freeData() call in the d'tor to be sure the last samples data set is properly released when closing the dialog.

  12. #10
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: is it a C++ error or Qt/Qwt related?

    Inserting freeData() where you say gives me strange graphs. Removing it works again right.

    Regards
    Giuseppe CalÃ

Similar Threads

  1. Related to header file
    By merry in forum Qt Programming
    Replies: 2
    Last Post: 10th May 2007, 13:03
  2. Question related to Q3DataTable
    By mitesh_modi in forum Qt Programming
    Replies: 0
    Last Post: 29th November 2006, 08:49
  3. Replies: 4
    Last Post: 3rd April 2006, 08:22
  4. Qt related questions and thoughts about getting job
    By AlexKiriukha in forum General Discussion
    Replies: 4
    Last Post: 26th January 2006, 12:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.