Page 1 of 3 123 LastLast
Results 1 to 20 of 59

Thread: How i can plot result in my GUI ?

  1. #1
    Join Date
    May 2011
    Posts
    122
    Thanks
    34
    Platforms
    Windows

    Default plot a curve during compilation

    Hello world,

    i have raed a tutorial which it speak about curve with QWT. In this tutorial i find this example to plot a curve during compilation:


    this is the code:

    Qt Code:
    1. #include <QApplication>
    2. #include <qwt_plot.h>
    3. #include <qwt_plot_curve.h>
    4.  
    5. int main(int argc, char** argv)
    6. {
    7. QApplication app(argc,argv);
    8. QwtPlot myPlot;
    9. QwtPlotCurve myCurve;
    10.  
    11. myCurve.attach(&myPlot);
    12.  
    13. QVector<double> x(5);
    14. QVector<double> y(5);
    15.  
    16. for(int i=0;i<5;i++)
    17. {
    18. x.append((double)i);
    19. y.append((double)(5-i));
    20. }
    21. myCurve.setData(x.data(),y.data(),x.size());
    22.  
    23. myPlot.replot();
    24. myPlot.show();
    25. return app.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 

    this is the error:

    ==> see attached file

    i don't why arguments are not good...

    how i can plot this curve please?

    thank you for your help

    (the help of QWT is very difficult for my, i am a real beginner, it is the reason why i ask this)
    Attached Images Attached Images

  2. #2
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: plot a curve during compilation

    Quote Originally Posted by 21did21 View Post
    i have raed a tutorial which it speak about curve with QWT. In this tutorial i find this example to plot a curve during compilation:
    this is a normal plot. nothing happens "during compilation".

    the code is written for an older version of qwt. you have to adapt it to the current version. maybe there are more/other errors...

    I suggest to take a look into the examples delivered with qwt. there are also quite simple plots.

    Felix

  3. #3
    Join Date
    May 2011
    Posts
    122
    Thanks
    34
    Platforms
    Windows

    Default Re: plot a curve during compilation

    thank you for your answer

    Quote Originally Posted by FelixB View Post
    this is a normal plot. nothing happens "during compilation".
    ok
    Quote Originally Posted by FelixB View Post
    the code is written for an older version of qwt. you have to adapt it to the current version.
    ok, but i don't know how i can adapt this... and i have read the exemple which are in Qwt6-0-0 but i don't understand anything, there is a lot of thing in each example, so i don't find the thing which are interesting for me...

    can you help me lease

  4. #4
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: plot a curve during compilation


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

    21did21 (30th May 2011)

  6. #5
    Join Date
    May 2011
    Posts
    122
    Thanks
    34
    Platforms
    Windows

    Default Re: plot a curve during compilation

    thank you a lot, i like this example !!!
    i understand a little now


    Added after 1 33 minutes:


    i have try to plote something but it don't work (i don't show something)

    This is my main.cpp
    Qt Code:
    1. #include <qapplication.h>
    2. #include <qwt_plot.h>
    3. #include <qwt_plot_curve.h>
    4. #include <vector>
    5. #include "fonction.h"
    6.  
    7. using namespace std;
    8.  
    9. int main(int argc, char **argv)
    10. {
    11. QApplication a(argc, argv);
    12. QwtPlot plot;
    13. QwtPlotCurve *curve = new QwtPlotCurve();
    14.  
    15. for (int i=1;i<10;i++)
    16. {
    17. QPolygonF points;
    18. vector<double>coord;
    19. coord=fonction();
    20.  
    21. points << QPointF( coord[0],coord[1] ) ;
    22.  
    23. curve->attach( &plot );
    24. plot.replot();
    25. }
    26.  
    27. plot.resize(600,400);
    28. plot.show();
    29.  
    30. return a.exec();
    31. }
    To copy to clipboard, switch view to plain text mode 

    and my fonction:
    Qt Code:
    1. #include <vector>
    2. #include <ctime> // to generate random point
    3. #include <cstdlib> // to generate random point
    4. #include "fonction.h"
    5.  
    6. using namespace std;
    7.  
    8. vector<double> fonction()
    9. {
    10. vector<double> points;
    11. srand(time(0));
    12. points.clear();
    13.  
    14. points.push_back(rand() % 1800);
    15. points.push_back(rand() % 500);
    16.  
    17. return points;
    18. }
    To copy to clipboard, switch view to plain text mode 

    and his header:
    Qt Code:
    1. #ifndef FONCTION_H
    2. #define FONCTION_H
    3.  
    4. std::vector<double> fonction();
    5.  
    6. #endif // FONCTION_H
    To copy to clipboard, switch view to plain text mode 


    can you explain me, how i can plot this curve. I want to différente version: with line between point and without line
    Last edited by 21did21; 31st May 2011 at 00:45.

  7. #6
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: plot a curve during compilation

    your plot does not show anything because it has no data. you have to points you want to plot in a QPolygonF. But what happens to this Polygon? have a look at the example uwe posted.

    btw, you should have the "attach" and "replot"-calls in your loop. that does not amke sense, it's sufficient to call both once after the loop.

  8. #7
    Join Date
    May 2011
    Posts
    122
    Thanks
    34
    Platforms
    Windows

    Default Re: plot a curve during compilation

    sorry but i don't understand very well because my english is bad...
    i don't understand how i can plot my points which are in the function "fonction". With my function i import 2 points for each loop, and after this a want to plot then with the previous.
    i think i forget a line "curve->setSamples( points ); " no ?

    (now i can't test i am not at home)
    Last edited by 21did21; 31st May 2011 at 14:37.

  9. #8
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: plot a curve during compilation

    yes, you have to call "setsamples" - after the loop. during the loop, you have to fill the QPolygonF with points.

    this is simple c++, so I won't give you code. You'll learn more by finding the solution by yourself to give you some hints: you need *one* QPolygonF, not 10. You want, that this QPOlygonF contains 10 QPoints. You want to plot these 10 points=one polygon...

  10. The following user says thank you to FelixB for this useful post:

    21did21 (31st May 2011)

  11. #9
    Join Date
    May 2011
    Posts
    122
    Thanks
    34
    Platforms
    Windows

    Default Re: plot a curve during compilation

    Quote Originally Posted by FelixB View Post
    yes, you have to call "setsamples" - after the loop. during the loop, you have to fill the QPolygonF with points.
    this is simple c++, so I won't give you code. You'll learn more by finding the solution by yourself to give you some hints: you need *one* QPolygonF, not 10. You want, that this QPOlygonF contains 10 QPoints. You want to plot these 10 points=one polygon...
    ok thank you for this explications, i test this evening...
    see you

    ps: juste one other think: is i don't want lines, just points what is the command which replace "Qpolygon" ?

  12. #10
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: plot a curve during compilation

    QPolygon is just an ordered collection of points - the data. how this data is painted is in the responsibility of QwtPlotCurve. Have a look at the API and search for something like "CurveStyle" - I don't know the exact method, the possible style types are n an enumeration.

  13. The following user says thank you to FelixB for this useful post:

    21did21 (31st May 2011)

  14. #11
    Join Date
    May 2011
    Posts
    122
    Thanks
    34
    Platforms
    Windows

    Default Re: plot a curve during compilation

    ok, i will see this

  15. #12
    Join Date
    May 2011
    Posts
    122
    Thanks
    34
    Platforms
    Windows

    Default Re: plot a curve during compilation

    I'm too bad I did not understand "Qpolygon" so I tried something else:

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include <qwt_plot.h>
    3. #include <qwt_plot_curve.h>
    4. #include <vector>
    5. #include "function.hpp"
    6. using namespace std;
    7. int main(int argc, char** argv)
    8. {
    9. QApplication app(argc,argv);
    10. QwtPlot myPlot;
    11. QwtPlotCurve myCurve;
    12. myCurve.attach(&myPlot);
    13. for (int i=1;i<10;i++)
    14. {
    15. vector<double> points;
    16. points=function();
    17. QVector<double> x(1);
    18. QVector<double> y(1);
    19. x.append( points[0]);
    20. y.append( points[1]);
    21. }
    22. myCurve.setSamples(x.data(),y.data(),x.size());
    23. myPlot.replot();
    24. myPlot.show();
    25. return app.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 
    function.cpp
    Qt Code:
    1. #include <vector>
    2. #include <ctime> // to generate random point
    3. #include <cstdlib> // to generate random point
    4. #include "function.hpp"
    5. using namespace std;
    6. vector<double> function()
    7. {
    8. vector<double> points;
    9. srand(time(0));
    10. points.clear();
    11. Sleep(5000);
    12. points.push_back(rand() % 1800);
    13. points.push_back(rand() % 500);
    14. return points;
    15. }
    To copy to clipboard, switch view to plain text mode 
    function.hpp
    Qt Code:
    1. #ifndef FUNCTION_HPP
    2. #define FUNCTION_HPP
    3. std::vector<double> function();
    4. #endif // FUNCTION_HPP
    To copy to clipboard, switch view to plain text mode 

    but this code say:

    X and Y are not declared in this scope, but i have declared this values???

    can you help me
    i bloc with Qwt until 3week just to plot a plot with some points

  16. #13
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: plot a curve during compilation

    you need some more c++-knowledge, your problems are not qwt-related. the vectors x and y are declared within scope of the loop. you can't access them when you left the loop. have you ever filled an array with a loop?

    in "fonction", you don't need the call "points.clear()" since the vector is empty when you create it. is there any reason for the "sleep(5000)"?

    ok, I'll help you with some code. I suggest to get back to QPolygonF. Then you can return a QPointF in "fonction()" directly. This is more intuitive and you can forget all the QVector stuff you don't need... I'll give you the modified loop and let you adapt "fonction()" by yourself. I'll help you when you can't manage that.

    Qt Code:
    1. QPolygonF polygon;
    2. for(unsigned int i=0; i<10; i++)
    3. polygon << fonction();
    4.  
    5. curve->setSamples(polygon); // maybe you need more parameters...
    To copy to clipboard, switch view to plain text mode 

  17. The following user says thank you to FelixB for this useful post:

    21did21 (1st June 2011)

  18. #14
    Join Date
    May 2011
    Posts
    122
    Thanks
    34
    Platforms
    Windows

    Default Re: plot a curve during compilation

    Quote Originally Posted by FelixB View Post
    is there any reason for the "sleep(5000)"?
    because my real code take a lot of time to calcul my values and here i want to simul this waiting time

  19. #15
    Join Date
    May 2011
    Posts
    122
    Thanks
    34
    Platforms
    Windows

    Default How i can plot result in my GUI ?

    Hello world,

    with your help i have done a GUI, but i add something to plut 6curves but i don't understand how i can send my results to the plot area....?

    Can you see and try my programm please? how i can launch the plot? can you correct me?

    it's difficult for me i am a beginner

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "MyMainWindow.h"
    3. #include <QGridLayout>
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7. MyMainWindow fenetre;
    8. fenetre.show();
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 
    MyMainWindow.h
    Qt Code:
    1. #ifndef MYMAINWINDOW_H
    2. #define MYMAINWINDOW_H
    3.  
    4. #include <QApplication>
    5. #include <QWidget>
    6. #include <QPushButton>
    7. #include <QMessageBox>
    8. #include <qwt_plot_curve.h>
    9. #include <qwt_plot.h>
    10.  
    11. class MyMainWindow : public QWidget
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit MyMainWindow(QWidget* parent = 0);
    17. void myRunSlot();
    18. void plotValuesFromFunction()
    19.  
    20. private:
    21. QPushButton *BUTTONRun;
    22. QPushButton *BUTTONQuit;
    23. QPushButton *BUTTONAbout;
    24. QPushButton *BUTTONContact;
    25.  
    26. QwtPlot *myPlot1;
    27. QwtPlot *myPlot2;
    28. QwtPlot *myPlot3;
    29. QwtPlot *myPlot4;
    30. QwtPlot *myPlot5;
    31. QwtPlot *myPlot6;
    32.  
    33. QwtPlotCurve myCurve1;
    34. QwtPlotCurve myCurve2;
    35. QwtPlotCurve myCurve3;
    36. QwtPlotCurve myCurve4;
    37. QwtPlotCurve myCurve5;
    38. QwtPlotCurve myCurve6;
    39.  
    40. };
    41. #endif // MYMAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    MyMainWindow.cpp
    Qt Code:
    1. #include "MyMainWindow.h"
    2. #include <QGridLayout>
    3. #include <QHBoxLayout>
    4. #include <qwt_plot_curve.h>
    5. #include "RunSimulation.h"
    6. #include <vector>
    7. MyMainWindow::MyMainWindow(QWidget* parent) : QWidget(parent)
    8. {
    9. QGridLayout* mainLayout = new QGridLayout();
    10. setLayout(mainLayout);
    11.  
    12. myPlot1 = new QwtPlot();
    13. myPlot2 = new QwtPlot();
    14. myPlot3 = new QwtPlot();
    15. myPlot4 = new QwtPlot();
    16. myPlot5 = new QwtPlot();
    17. myPlot6 = new QwtPlot();
    18.  
    19. myPlot1->setMinimumSize(200, 100);
    20. myPlot2->setMinimumSize(200, 100);
    21. myPlot3->setMinimumSize(200, 100);
    22. myPlot4->setMinimumSize(200, 100);
    23. myPlot5->setMinimumSize(200, 100);
    24. myPlot6->setMinimumSize(200, 100);
    25.  
    26. QWidget* buttonWidget = new QWidget();
    27. QGridLayout* buttonLayout = new QGridLayout();
    28.  
    29. BUTTONRun = new QPushButton("RUN");
    30. BUTTONQuit = new QPushButton("STOP");
    31. BUTTONAbout = new QPushButton("About");
    32. BUTTONContact = new QPushButton("Contact");
    33.  
    34. buttonLayout->addWidget(BUTTONRun ,0,0);
    35. buttonLayout->addWidget(BUTTONQuit ,0,1);
    36. buttonLayout->addWidget(BUTTONAbout ,1,0);
    37. buttonLayout->addWidget(BUTTONContact,1,1);
    38.  
    39. buttonWidget->setLayout(buttonLayout);
    40.  
    41. mainLayout->addWidget(myPlot1 ,0,0,1,1);
    42. mainLayout->addWidget(myPlot2 ,0,1,1,1);
    43. mainLayout->addWidget(myPlot3 ,0,2,1,1);
    44. mainLayout->addWidget(myPlot4 ,1,0,1,1);
    45. mainLayout->addWidget(myPlot5 ,1,1,1,1);
    46. mainLayout->addWidget(myPlot6 ,1,2,1,1);
    47. mainLayout->addWidget(buttonWidget ,0,3,1,1);
    48.  
    49. myCurve1.attach(&myPlot1);
    50. myCurve2.attach(&myPlot2);
    51. myCurve3.attach(&myPlot3);
    52. myCurve4.attach(&myPlot4);
    53. myCurve5.attach(&myPlot5);
    54. myCurve6.attach(&myPlot6);
    55.  
    56. QObject::connect(BUTTONQuit, SIGNAL(clicked()), qApp, SLOT(quit()));
    57. QObject::connect(BUTTONAbout, SIGNAL(clicked()), qApp, SLOT(aboutQt()));
    58. QObject::connect(BUTTONRun, SIGNAL(clicked()), qApp, SLOT(myRunSlot()));
    59. }
    60. void MyMainWindow::myRunSlot()
    61. {
    62. RunSimulation();
    63. }
    64. void MyMainWindow::plotValuesFromFunction()
    65. {
    66. QVector<double> x(1);
    67. QVector<double> y(1);
    68. vector<double> points;
    69.  
    70. x.append( points[0] );
    71. y.append( points[1] );
    72.  
    73. myCurve1.setSamples(x.data(),y.data(),x.size());
    74. myCurve2.setSamples(x.data(),y.data(),x.size());
    75. myCurve3.setSamples(x.data(),y.data(),x.size());
    76. myCurve4.setSamples(x.data(),y.data(),x.size());
    77. myCurve5.setSamples(x.data(),y.data(),x.size());
    78. myCurve6.setSamples(x.data(),y.data(),x.size());
    79. myPlot.replot();
    80. myPlot.show();
    81. }
    To copy to clipboard, switch view to plain text mode 
    RunSimulation.h
    Qt Code:
    1. #ifndef RUNSIMULATION_H
    2. #define RUNSIMULATION_H
    3.  
    4. void RunSimulation();
    5.  
    6. #endif // RUNSIMULATION_H
    To copy to clipboard, switch view to plain text mode 
    RunSimulation.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include <qwt_plot.h>
    3. #include <qwt_plot_curve.h>
    4. #include <vector>
    5. #include "function.h"
    6. #include "RunSimulation.h"
    7. using namespace std;
    8. void RunSimulation()
    9. {
    10. vector<double> points;
    11. for (int i=1;i<10;i++)
    12. {
    13. points.clear();
    14. points=function();
    15. //!! for each "i" i want to send result to the 6plots... but i don't know how...?
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 
    function.h
    Qt Code:
    1. #ifndef FUNCTION_HPP
    2. #define FUNCTION_HPP
    3. std::vector<double> function();
    4. #endif // FUNCTION_HPP
    To copy to clipboard, switch view to plain text mode 
    function.cpp
    Qt Code:
    1. #include <vector>
    2. #include <ctime> // to generate random point
    3. #include <cstdlib> // to generate random point
    4. #include "function.h"
    5. #include "windows.h"
    6. using namespace std;
    7. vector<double> function()
    8. {
    9. vector<double> points;
    10. srand(time(0));
    11. points.clear();
    12. Sleep(1);
    13. points.push_back(rand() % 1800);
    14. points.push_back(rand() % 500);
    15. return points;
    16. }
    To copy to clipboard, switch view to plain text mode 

    it 's very important for me to plot this but it's very difficult, i am not an "informaticien"
    i hope you can help me

  20. #16
    Join Date
    May 2011
    Posts
    122
    Thanks
    34
    Platforms
    Windows

    Default Re: How i can plot result in my GUI ?

    this code can work, but it difficult for me to send data from the "function" to my plots

    i hope that you can help me,

    thank you

  21. #17
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How i can plot result in my GUI ?

    i don't understand how i can send my results to the plot area....?
    Looks like the plot area belongs to mainWindow, so in order to send it some data, you'll need to expose main window object somehow.
    Where the RunSimulation() method is called ? You want to call it, for example, on button click ?

  22. #18
    Join Date
    May 2011
    Posts
    122
    Thanks
    34
    Platforms
    Windows

    Default Re: How i can plot result in my GUI ?

    thank you for your answer
    Quote Originally Posted by stampede View Post
    Looks like the plot area belongs to mainWindow, so in order to send it some data, you'll need to expose main window object somehow.
    sorry, i don't understand this: "you'll need to expose main window object somehow"

    Quote Originally Posted by stampede View Post
    Where the RunSimulation() method is called ? You want to call it, for example, on button click ?
    when i click on the button "Run" i tell the function "RunSimulation" and the code works

  23. #19
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How i can plot result in my GUI ?

    Simple solution is to make the RunSimulation() function return some data, so you can use it in MainWindow:
    Qt Code:
    1. vector< vector<double> > RunSimulation()
    2. {
    3. const int count = 9;
    4. vector< vector<double> > data;
    5. for (int i=0;i<count;i++)
    6. {
    7. data.push_back( function() );
    8. }
    9. return data;
    10. }
    11.  
    12. // in main window:
    13. void MyMainWindow::myRunSlot()
    14. {
    15. vector< vector<double> > points = RunSimulation();
    16. // display the points in plot
    17. }
    To copy to clipboard, switch view to plain text mode 
    In order to display the points, create a method that takes a vector<double> as an argument ( very simple modification of the plotValuesFromFunction() ) and call it for every vector in points vector.
    Btw. use Qt container classes if you can.
    Last edited by stampede; 2nd June 2011 at 22:01.

  24. #20
    Join Date
    May 2011
    Posts
    122
    Thanks
    34
    Platforms
    Windows

    Default Re: How i can plot result in my GUI ?

    thank you for your help, but this is two remarks:

    1°)
    => i can't change "RunSimulation" because in the reallity i don't work with this function, i work with a function "RunSimulation" that is huge and it's a void type.
    => in RunSimulation i don't calculate something, just i tell other method or function; it's like a "main.cpp"
    so i propose this example because it's very close to me but without the all code, that is very long

    2°)
    for me it's interesting to see the evolution of my values during the calcul, so i don't want to see results at the end of calcul but step by step

    i hope that this problem is not too difficult ?

Similar Threads

  1. QRegExp - get only last result
    By kabanek in forum Newbie
    Replies: 2
    Last Post: 3rd November 2010, 23:17
  2. How to display result
    By sksingh73 in forum Newbie
    Replies: 1
    Last Post: 7th June 2010, 09:39
  3. QSqlQuery return result
    By arpspatel in forum Qt Programming
    Replies: 2
    Last Post: 9th April 2010, 08:55
  4. Result of DBUS call
    By conexion2000 in forum Qt Programming
    Replies: 4
    Last Post: 28th July 2009, 09:34
  5. Replies: 7
    Last Post: 22nd September 2008, 23:05

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.