Page 1 of 2 12 LastLast
Results 1 to 20 of 59

Thread: How i can plot result in my GUI ?

Hybrid View

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

    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

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

    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

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

    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 ?

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

    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

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

    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 21:01.

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

    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 ?

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

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

    Ok, but you'll need to make some changes to the RunSimulation code anyway.
    Another way, create an instance of notifier class, based on QObject, and use it to send signals with new data from RunSimulation method, something like this:
    Qt Code:
    1. // RunSimulation.h
    2.  
    3. class Notifier : public QObject{
    4. Q_OBJECT
    5. public:
    6. Notifier( QObject * parent = NULL ) : QObject(parent){}
    7. void sendData( const vector<double>& data ){
    8. emit newData(data);
    9. }
    10.  
    11. static Notifier& instance(){
    12. static Notifier inst;
    13. return inst;
    14. }
    15.  
    16. signals:
    17. void newData( const vector<double>& data );
    18. };
    19.  
    20. //RunSimulation.cpp
    21.  
    22. void RunSimulation()
    23. {
    24. vector<double> points;
    25. for (int i=1;i<10;i++)
    26. {
    27. points.clear();
    28. points=function();
    29. Notifier::instance().sendData(points);
    30. }
    31. }
    32.  
    33. // and in main:
    34. void MyMainWindow::myRunSlot()
    35. {
    36. connect( &Notifier::instance(), SIGNAL(newData(const vector<double>&)), this, SLOT(plotData(const vector<double>&)) ); // you'll need to create this slot to plot incoming data
    37. RunSimulation();
    38. }
    To copy to clipboard, switch view to plain text mode 
    You may not like the idea of a kind of singleton, but it's only an example of how you can connect your simulation method with main gui. More brute way could be to simply pass this pointer to RunSimulation method:
    Qt Code:
    1. //
    2. #include "MyMainWindow.h"
    3.  
    4. void RunSimulation( MyMainWindow * mainWindow ){
    5. vector<double> points;
    6. for (int i=1;i<10;i++)
    7. {
    8. points.clear();
    9. points=function();
    10. mainWindow->plotData(points);
    11. }
    12. }
    13. // main:
    14. void MyMainWindow::myRunSlot()
    15. {
    16. RunSimulation(this);
    17. }
    To copy to clipboard, switch view to plain text mode 
    Again, you won't escape changing the RunSimulation method, because all the data you compute there is local, you need to expose this data somehow.
    Another way could be to take advantage of Qt event processing - create a subclass of QEvent and send it with QCoreApplication::sendEvent() to mainWindow.

  8. The following user says thank you to stampede for this useful post:

    21did21 (3rd June 2011)

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

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

    thank for your answer! i will try this, i think it's good for my problem thank a lot.
    i will inform you
    see you

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

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

    Quote Originally Posted by stampede View Post
    // and in main:
    void MyMainWindow::myRunSlot()
    {
    connect( &Notifier::instance(), SIGNAL(newData(const vector<double>&)), this, SLOT(plotData(const vector<double>&)) ); // you'll need to create this slot to plot incoming data
    RunSimulation();
    }
    [/CODE]
    are ou sure? i test but it give problem "this" can point to a function, and main is a function...

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

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

    this is an instance of MainWindow class. New data signal should be connected to plotting slot.
    Don't take it literally if I write "in main:" in comment, I meant mainWindow gui code.

  12. The following user says thank you to stampede for this useful post:

    21did21 (6th June 2011)

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

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

    i have try this, but it still don't work.
    => but now i have less problem of compilation


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

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

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

    What do you mean by "don't work" ?

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

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

    thank you for your help

    Quote Originally Posted by stampede View Post
    What do you mean by "don't work" ?
    yes, excuse me.
    when i compile my programm the GUI appear but when i click on the "run button" the points don't appear on the chart

  16. #14
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,349
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

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

    21did21: I think your problem is that you do not understand enough about C++ to get started. There are so many errors in your code that it is difficult for anyone here to help you (except to write the code for you). We are trying to be helpful, but we can't teach you C++.

    Is there someone you know who has some C++ knowledge who speaks your native language? It would be much better for you to sit with that person and ask them for help instead of trying to solve it here.

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

    Default plot during execution

    Hello world,

    i have a code which it plot in several windows values but it don't plot during execution, just à the end...

    how i can improve this to plot point by point?

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "MyMainWindow.h"
    3. #include <QGridLayout>
    4. #include "RunSimulation.h"
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8. MyMainWindow fenetre;
    9. fenetre.show();
    10. return app.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    MyMainWindows
    Qt Code:
    1. ---------.h---------
    2. #ifndef MYMAINWINDOW_H
    3. #define MYMAINWINDOW_H
    4.  
    5. #include <QApplication>
    6. #include <QWidget>
    7. #include <QPushButton>
    8. #include <QMessageBox>
    9. #include <qwt_plot_curve.h>
    10. #include <qwt_plot.h>
    11.  
    12. class MyMainWindow : public QWidget
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit MyMainWindow(QWidget* parent = 0);
    18. void plotValuesFromFunction(std::vector<double>);
    19. public slots:
    20. void myRunSlot();
    21. private:
    22. QPushButton *BUTTONRun;
    23. QPushButton *BUTTONQuit;
    24. QPushButton *BUTTONAbout;
    25. QPushButton *BUTTONContact;
    26.  
    27. QwtPlot *myPlot1;
    28. QwtPlot *myPlot2;
    29. QwtPlot *myPlot3;
    30. QwtPlot *myPlot4;
    31. QwtPlot *myPlot5;
    32. QwtPlot *myPlot6;
    33.  
    34. QwtPlotCurve myCurve1;
    35. QwtPlotCurve myCurve2;
    36. QwtPlotCurve myCurve3;
    37. QwtPlotCurve myCurve4;
    38. QwtPlotCurve myCurve5;
    39. QwtPlotCurve myCurve6;
    40.  
    41. };
    42. #endif // MYMAINWINDOW_H
    43. ---------.cpp------
    44. #include "MyMainWindow.h"
    45. #include <QGridLayout>
    46. #include <QHBoxLayout>
    47. #include <qwt_plot_curve.h>
    48. #include "RunSimulation.h"
    49. #include <vector>
    50.  
    51. using namespace std;
    52. MyMainWindow::MyMainWindow(QWidget* parent) : QWidget(parent)
    53. {
    54. QGridLayout* mainLayout = new QGridLayout();
    55. setLayout(mainLayout);
    56.  
    57. myPlot1 = new QwtPlot();
    58. myPlot2 = new QwtPlot();
    59. myPlot3 = new QwtPlot();
    60. myPlot4 = new QwtPlot();
    61. myPlot5 = new QwtPlot();
    62. myPlot6 = new QwtPlot();
    63.  
    64. myPlot1->setMinimumSize(200, 100);
    65. myPlot2->setMinimumSize(200, 100);
    66. myPlot3->setMinimumSize(200, 100);
    67. myPlot4->setMinimumSize(200, 100);
    68. myPlot5->setMinimumSize(200, 100);
    69. myPlot6->setMinimumSize(200, 100);
    70.  
    71. QWidget* buttonWidget = new QWidget();
    72. QGridLayout* buttonLayout = new QGridLayout();
    73.  
    74. BUTTONRun = new QPushButton("RUN");
    75. BUTTONQuit = new QPushButton("STOP");
    76. BUTTONAbout = new QPushButton("About");
    77. BUTTONContact = new QPushButton("Contact");
    78.  
    79. buttonLayout->addWidget(BUTTONRun ,0,0);
    80. buttonLayout->addWidget(BUTTONQuit ,0,1);
    81. buttonLayout->addWidget(BUTTONAbout ,1,0);
    82. buttonLayout->addWidget(BUTTONContact,1,1);
    83.  
    84. buttonWidget->setLayout(buttonLayout);
    85.  
    86. mainLayout->addWidget(myPlot1 ,0,0,1,1);
    87. mainLayout->addWidget(myPlot2 ,0,1,1,1);
    88. mainLayout->addWidget(myPlot3 ,0,2,1,1);
    89. mainLayout->addWidget(myPlot4 ,1,0,1,1);
    90. mainLayout->addWidget(myPlot5 ,1,1,1,1);
    91. mainLayout->addWidget(myPlot6 ,1,2,1,1);
    92. mainLayout->addWidget(buttonWidget ,0,3,1,1);
    93.  
    94. myCurve1.attach(myPlot1);
    95. myCurve2.attach(myPlot2);
    96. myCurve3.attach(myPlot3);
    97. myCurve4.attach(myPlot4);
    98. myCurve5.attach(myPlot5);
    99. myCurve6.attach(myPlot6);
    100.  
    101. QObject::connect(BUTTONQuit, SIGNAL(clicked()), qApp, SLOT(quit()));
    102. QObject::connect(BUTTONAbout, SIGNAL(clicked()), qApp, SLOT(aboutQt()));
    103. QObject::connect(BUTTONRun, SIGNAL(clicked()), qApp, SLOT(myRunSlot()));
    104. }
    105. void MyMainWindow::plotValuesFromFunction(vector<double>points)
    106. {
    107. QVector<double> x(1);
    108. QVector<double> y(1);
    109.  
    110. x.append( points[0] );
    111. y.append( points[1] );
    112.  
    113. myCurve1.setSamples(x.data(),y.data(),x.size());
    114. myCurve2.setSamples(x.data(),y.data(),x.size());
    115. myCurve3.setSamples(x.data(),y.data(),x.size());
    116. myCurve4.setSamples(x.data(),y.data(),x.size());
    117. myCurve5.setSamples(x.data(),y.data(),x.size());
    118. myCurve6.setSamples(x.data(),y.data(),x.size());
    119. myPlot1->replot();
    120. myPlot2->replot();
    121. myPlot3->replot();
    122. myPlot4->replot();
    123. myPlot5->replot();
    124. myPlot6->replot();
    125. myPlot1->show();
    126. myPlot2->show();
    127. myPlot3->show();
    128. myPlot4->show();
    129. myPlot5->show();
    130. myPlot6->show();
    131. }
    132. void MyMainWindow::myRunSlot()
    133. {
    134. RunSimulation(this);
    135. }
    To copy to clipboard, switch view to plain text mode 

    RunSimulation
    Qt Code:
    1. --.h--------
    2. #ifndef RUNSIMULATION_H
    3. #define RUNSIMULATION_H
    4. void RunSimulation();
    5. #endif // RUNSIMULATION_H
    6. -.cpp-------
    7. #include <QApplication>
    8. #include <qwt_plot.h>
    9. #include <qwt_plot_curve.h>
    10. #include <vector>
    11. #include "function.h"
    12. #include "RunSimulation.h"
    13. #include "MyMainWindow.h"
    14.  
    15. using namespace std;
    16. void RunSimulation( MyMainWindow * mainWindow )
    17. {
    18. vector<double> points;
    19. for (int i=1;i<10;i++)
    20. {
    21. points.clear();
    22. points=function();
    23. mainWindow->plotValuesFromFunction(points);
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

    fonction
    Qt Code:
    1. --.h-------
    2. #ifndef FUNCTION_HPP
    3. #define FUNCTION_HPP
    4. std::vector<double> function();
    5. #endif // FUNCTION_HPP
    6. --.cpp-----
    7. #include <vector>
    8. #include <ctime> // to generate random point
    9. #include <cstdlib> // to generate random point
    10. #include "function.h"
    11. #include "windows.h"
    12. using namespace std;
    13. vector<double> function()
    14. {
    15. vector<double> points;
    16. srand(time(0));
    17. points.clear();
    18. Sleep(5000);
    19. points.push_back(rand() % 1800);
    20. points.push_back(rand() % 500);
    21. return points;
    22. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by 21did21; 9th June 2011 at 16:43.

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

    Default Re: plot during execution

    i think i forget one "replot()" no?

    => but where?

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

    Default Re: plot during execution

    your code can't work. why do you clear the points-vector in each loop? I already gave you the solution how to fix that...

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

    and you don't need to call "myplot->show()" in plotValuesFromFunction, it's sufficient to do that once in the constructor.

    PS: you already opened one thread for this topic. it's very bad style to create a second thread only because nobody answers in your first thread.

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

    21did21 (10th June 2011)

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

    Default Re: plot during execution

    i clear previous point because i want:
    => run calcul and get 1point
    => plot this point
    => run calcul and get 1 point
    => plot this point
    ....
    for each points

    and i don't want a polynom, i just want a plot with only points. (Dots style; i think )

    ps: I made this post because the other start to be very rough and can not distinguish what I was looking for
    Last edited by 21did21; 10th June 2011 at 09:56.

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

    Default Re: plot during execution

    it makes no difference whether you use a polygon or a vector. that has no effect on the style. you can set the style by using "QwtPlotCurve::setStyle (CurveStyle style)".

    you plot exactly one point after each calculation. the previously calculated (and plotted) points will be deleted. is that really what you want? or do you want to keep all points in the plot?

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

    21did21 (10th June 2011)

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

    Default Re: plot during execution

    Quote Originally Posted by FelixB View Post
    it makes no difference whether you use a polygon or a vector. that has no effect on the style. you can set the style by using "QwtPlotCurve::setStyle (CurveStyle style)".
    ok . thank you

    Quote Originally Posted by FelixB View Post
    you plot exactly one point after each calculation. the previously calculated (and plotted) points will be deleted. is that really what you want? or do you want to keep all points in the plot?
    ok! i understand why i must not clear the vector!
    => yes i want to plot one point and keep the previous

    thank you for yur help

    => i try, and i will keep informed

    EDIT:
    i think now i understand the mistake, i try it and i will keep you informed
    Last edited by 21did21; 10th June 2011 at 14:13.

Similar Threads

  1. QRegExp - get only last result
    By kabanek in forum Newbie
    Replies: 2
    Last Post: 3rd November 2010, 22:17
  2. How to display result
    By sksingh73 in forum Newbie
    Replies: 1
    Last Post: 7th June 2010, 08:39
  3. QSqlQuery return result
    By arpspatel in forum Qt Programming
    Replies: 2
    Last Post: 9th April 2010, 07:55
  4. Result of DBUS call
    By conexion2000 in forum Qt Programming
    Replies: 4
    Last Post: 28th July 2009, 08:34
  5. Replies: 7
    Last Post: 22nd September 2008, 22: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
  •  
Qt is a trademark of The Qt Company.