Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 59

Thread: How i can plot result in my GUI ?

  1. #21
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,393
    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 ?

    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.

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

    21did21 (3rd June 2011)

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

    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

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

    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...

  5. #24
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,393
    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 ?

    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.

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

    21did21 (6th June 2011)

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

    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 

  8. #26
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,393
    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 ?

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

  9. #27
    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

    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

  10. #28
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,393
    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 ?

    Is the slot method called ? Add some debugging messages to know what's going on during execution.

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

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

    Quote Originally Posted by stampede View Post
    Is the slot method called ? Add some debugging messages to know what's going on during execution.
    i don't know if the slot method is called.

    I don't know how i can show message during execution because "cout" don't work because i am not in a console projet...

    can you explain me how i can do this

  12. #30
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,393
    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 ?

    If you are using windows, add CONFIG += console to project's .pro file, then you can output to stdout any message using qDebug()
    I think it will work without the extra CONFIG if you run your app with gdb, qDebug() output should be visible in debugger message window

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

    21did21 (11th June 2011)

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

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

    thank you,i haven't compilation's problem but where the console appear?

    during debug i have this error (remark: exempleForum is the name of my project):
    Object::connect: No such slot QApplication::myRunSlot() in ..\2exempleForum\MyMainWindow.cpp:61
    Object::connect: (receiver name: 'exempleForum')
    main


    with this code i just change the prototype of the function :

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

  15. #32
    Join Date
    May 2011
    Posts
    21
    Thanks
    1
    Thanked 5 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

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

    QObject::connect(BUTTONRun, SIGNAL(clicked()), qApp, SLOT(myRunSlot()));
    qApp (QApplication instance) doesn't have a myRunSlot() slot. "this" has one.

    QObject::connect(a, SIGNAL(a()), this, SLOT(b())); = connect(a, SIGNAL(a()), SLOT(b()));

  16. The following user says thank you to Troudhyl for this useful post:

    21did21 (7th June 2011)

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

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

    i have replace this:
    QObject::connect(BUTTONRun, SIGNAL(clicked()), qApp, SLOT(myRunSlot()));

    by:

    QObject::connect(BUTTONRun, SIGNAL(clicked()), this, SLOT(myRunSlot()));

    now i haven't error but it still don't works...

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

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

    you have to declare "myRunSlot()" as slot:

    Qt Code:
    1. public slots:
    2. void myRunSlot();
    To copy to clipboard, switch view to plain text mode 

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

    21did21 (8th June 2011)

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

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

    yes it's a mistake! thanks a lot!!! it resolve a problem

    but one problem continue:
    => my curve appear just at the end, the point are not ploting during execution
    Last edited by 21did21; 8th June 2011 at 09:24.

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

    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.

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

    Default Re: plot during execution

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

    => but where?

  23. #38
    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 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.

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

    21did21 (10th June 2011)

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

    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.

  26. #40
    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 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?

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

    21did21 (10th June 2011)

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.