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

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

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

  4. #4
    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 ?

  5. #5
    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 ?

    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.

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

    21did21 (3rd June 2011)

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

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

  9. #8
    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 ?

    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.

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

    21did21 (6th June 2011)

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

  12. #10
    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 ?

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

  13. #11
    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

  14. #12
    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 ?

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

  15. #13
    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

  16. #14
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,233
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    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.

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.