Results 1 to 4 of 4

Thread: Problems with plotting straight lines with QWT

  1. #1

    Default Problems with plotting straight lines with QWT

    Hello,

    I am new to the world of GUI and I am having some problems with building a programm to plot straight lines.
    It would be very nice, if somebody could help me...
    I am using Visual Studio 2010 Express.
    Both Qt and QWT are succesfully integrated and are tested to work properly.
    I thought it wold work like this:

    - The parameters (Offset + Gradient) of the straight line are committed in the Main Window

    - Pushing the Plot-Button should call a Constructor, that creates a QWT PLot, called Graph,
    and inserts a straight line with the committed paramters. (Plot.h/cpp)

    - Furthermore a QDialog should be created

    -The previously created Graph should now be inserted in the QDialog

    - After all the new Window should open and display the straight line


    My Problem is, that after succesfully compiling and linking the mainwindow opens, but pushing the plot-button
    results in the programms shutdown with errorcode 1.
    If I leave out the call of the Graph constructor in Plotm::draw, the programm runs completely succesfully but the new window is empty of course.
    So in my oppinion I do something wrong in the constructor...

    Thank you for your help !

    Plotm.h:
    Qt Code:
    1. #ifndef PLOTM_H
    2.  
    3. #define PLOTM_H
    4. #include "ui_Plot10.h"
    5. #include <qdialog.h>
    6.  
    7. class Plotm : public QMainWindow, public Ui::MainWindow
    8. {
    9.  
    10. Q_OBJECT
    11.  
    12. public:
    13.  
    14. Plotm (QMainWindow *parent = 0);
    15.  
    16. ~ Plotm ();
    17.  
    18. private slots:
    19.  
    20. void draw();
    21. };
    22.  
    23. #endif //PLOTM_H
    To copy to clipboard, switch view to plain text mode 
    Plotm.cpp:
    Qt Code:
    1. #include "Plotm.h"
    2. #include "Plot.h"
    3.  
    4.  
    5. Plotm::Plotm(QMainWindow *parent) : QMainWindow(parent)
    6. {
    7. setupUi(this);
    8. xmi-> setText("0");
    9. ymi-> setText("0");
    10.  
    11. connect(plotb, SIGNAL(clicked()), this, SLOT(draw()));
    12. }
    13.  
    14. Plotm::~Plotm()
    15. {}
    16.  
    17. void Plotm::draw()
    18. {
    19. Graph* plot ;
    20. QDialog* nw;
    21. nw = new QDialog(this, Qt::Window);
    22.  
    23. int ava; //Gradient
    24. int bva; //Offset
    25.  
    26. ava = aval->text().toDouble();
    27. bva = bval->text().toDouble();
    28.  
    29.  
    30. plot = new Graph(ava,bva,nw);
    31. plot->show();
    32. nw->show();
    33. }
    To copy to clipboard, switch view to plain text mode 

    Plot.h:
    Qt Code:
    1. #ifndef PLOT_H
    2. #define PLOT_H
    3.  
    4. #include <qapplication.h>
    5. #include <qlayout.h>
    6. #include <qwt_plot.h>
    7. #include <qwt_plot_marker.h>
    8. #include <qwt_plot_curve.h>
    9. #include <qwt_legend.h>
    10. #include <qwt_series_data.h>
    11. #include <qwt_plot_canvas.h>
    12. #include <qwt_plot_panner.h>
    13. #include <qwt_plot_magnifier.h>
    14. #include <qwt_text.h>
    15. #include <qwt_math.h>
    16. #include <math.h>
    17. #include <qwt_symbol.h>
    18. #include <QtGui>
    19.  
    20. class Graph : public QwtPlot
    21. {
    22.  
    23. public:
    24. int aval;//Gradient
    25. int bval;//Offset
    26.  
    27. double xval[100];
    28. double yval[100];
    29.  
    30. //Constructor which commits the parameters of the straight line to create
    31. Graph(int avala,int bvala, QWidget* parent);
    32. };
    33.  
    34. #endif
    To copy to clipboard, switch view to plain text mode 
    Plot.cpp:
    Qt Code:
    1. #include <qapplication.h>
    2. #include <qlayout.h>
    3. #include <qwt_plot.h>
    4. #include <qwt_plot_marker.h>
    5. #include <qwt_plot_curve.h>
    6. #include <qwt_legend.h>
    7. #include <qwt_series_data.h>
    8. #include <qwt_plot_canvas.h>
    9. #include <qwt_plot_panner.h>
    10. #include <qwt_plot_magnifier.h>
    11. #include <qwt_text.h>
    12. #include <qwt_math.h>
    13. #include <math.h>
    14. #include <qwt_symbol.h>
    15. #include "Plot.h"
    16. #include <qwt_plot_grid.h>
    17. #include "Plotm.h"
    18. #include "Plot.h"
    19.  
    20.  
    21. Graph::Graph(int avala,int bvala,QWidget *parent)
    22. {
    23.  
    24. aval = avala;
    25. bval = bvala;
    26.  
    27. setAutoFillBackground( true );
    28. setPalette( QPalette( QColor( 165, 193, 228 ) ) );
    29.  
    30. setTitle("Plot 1.0");
    31. insertLegend(new QwtLegend(), QwtPlot::RightLegend);
    32.  
    33. // axes
    34. setAxisTitle(xBottom, "x -->" );
    35. setAxisScale(xBottom, 0, 10);
    36.  
    37. setAxisTitle(yLeft, "y -->");
    38. setAxisScale(yLeft, 0, 10);
    39.  
    40. // canvas
    41. canvas()->setLineWidth( 1 );
    42. canvas()->setFrameStyle( QFrame::Box | QFrame::Plain );
    43. canvas()->setBorderRadius( 15 );
    44.  
    45. QPalette canvasPalette( Qt::white );
    46. canvasPalette.setColor( QPalette::Foreground, QColor( 133, 190, 232 ) );
    47. canvas()->setPalette( canvasPalette );
    48.  
    49. for(int i=0; i<100;i++)
    50. {
    51. xval[i] = double(i) * 1/10;
    52. yval[i] = this->aval * xval[i] + this->bval;
    53. }
    54.  
    55. // Insert new curves
    56.  
    57. QwtPlotCurve * d_curves = new QwtPlotCurve("y = a*x+b");
    58. d_curves->setRenderHint(QwtPlotItem::RenderAntialiased);
    59. d_curves->setLegendAttribute(QwtPlotCurve::LegendShowLine, true);
    60. d_curves->setPen(QPen(Qt::green));
    61. d_curves->setRawSamples(xval, yval, 100);
    62. d_curves->attach(this);
    63.  
    64. replot();
    65. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2012
    Posts
    17
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problems with plotting straight lines with QWT

    I think you can't attach curves in the constructor of your own QwtPlot

    Suggestions:
    - don't attach curves in the constructor
    - don't replot in the contructor
    - your QwtPlot constructor has to call QwtPlot constructor: Graph::Graph(...., QWidget *parent) : QwtPlot(parent)
    - I think you need a QLayoutXXX to attach your plot to your QDialog

    regards

  3. #3

    Default Re: Problems with plotting straight lines with QWT

    Hi,

    thanks for your help,
    I tried to correct the things you mentioned.
    However, the same Problem occurs.
    My Code looks like this now:

    Plot.h
    Qt Code:
    1. class Graph : public QwtPlot
    2. {
    3.  
    4. public:
    5.  
    6. Graph(QWidget* parent);
    7. QwtPlot *myPlot;
    8. };
    9.  
    10. #endif
    To copy to clipboard, switch view to plain text mode 

    Plot.cpp
    Qt Code:
    1. Graph::Graph(QWidget *parent) : QwtPlot (parent)
    2. {
    3. myPlot= new QwtPlot(parent);
    4.  
    5. myPlot->setAutoFillBackground( true );
    6. myPlot->setPalette( QPalette( QColor( 165, 193, 228 ) ) );
    7.  
    8. myPlot->setTitle("Plot 1.0");
    9. myPlot->insertLegend(new QwtLegend(), QwtPlot::RightLegend);
    10.  
    11. // axes
    12. myPlot->setAxisTitle(xBottom, "x -->" );
    13. myPlot->setAxisScale(xBottom, 0, 10);
    14.  
    15. myPlot->setAxisTitle(yLeft, "y -->");
    16. myPlot->setAxisScale(yLeft, 0, 10);
    17.  
    18. myPlot->setAutoReplot(true);
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 

    Plotm.h
    Qt Code:
    1. #define PLOTM_H
    2. #include "ui_Plot10.h"
    3. #include <qdialog.h>
    4.  
    5. class Plotm : public QMainWindow, public Ui::MainWindow
    6. {
    7.  
    8. Q_OBJECT
    9.  
    10. public:
    11.  
    12. Plotm (QMainWindow *parent = 0);
    13.  
    14. ~ Plotm ();
    15.  
    16. int avalue;
    17. int bvalue;
    18. int xmax;
    19. int xmin;
    20. int ymax;
    21. int ymin;
    22. double xval[100];
    23. double yval[100];
    24.  
    25. private slots:
    26.  
    27. void draw();
    28. };
    29.  
    30. #endif //PLOTM_H
    To copy to clipboard, switch view to plain text mode 

    Plotm.cpp
    Qt Code:
    1. #include "Plotm.h"
    2. #include "Plot.h"
    3.  
    4.  
    5. Plotm::Plotm(QMainWindow *parent) : QMainWindow(parent)
    6. {
    7. setupUi(this);
    8. xmi-> setText("0");
    9. ymi-> setText("0");
    10.  
    11. connect(plotb, SIGNAL(clicked()), this, SLOT(draw()));
    12. }
    13.  
    14. Plotm::~Plotm()
    15. {}
    16.  
    17. void Plotm::draw()
    18. {
    19. Graph* plot ;
    20. QDialog* nw;
    21. QHBoxLayout *layout;
    22.  
    23. nw = new QDialog(this, Qt::Window);
    24. plot = new Graph(nw);
    25. layout = new QHBoxLayout(nw);
    26.  
    27. layout->setContentsMargins( 0, 0, 0, 0 );
    28. layout->addWidget(plot);
    29.  
    30.  
    31. int ava;
    32. int bva;
    33. int xmax;
    34. int xmin;
    35. int ymax;
    36. int ymin;
    37.  
    38. ava = aval->text().toDouble();
    39. bva = bval->text().toDouble();
    40. xmax = xma->text().toDouble();
    41. xmin = xmi->text().toDouble();
    42. ymax = yma->text().toDouble();
    43. ymin = ymi->text().toDouble();
    44.  
    45. double xval[100];
    46. double yval[100];
    47.  
    48. for(int i=0; i<100;i++)
    49. {
    50. xval[i] = double(i) * ((xmax - xmin) / 100);
    51. yval[i] = ava * xval[i] + bva;
    52. }
    53.  
    54. QwtPlotCurve * d_curves = new QwtPlotCurve("y = a*x+b");
    55. d_curves->setRenderHint(QwtPlotItem::RenderAntialiased);
    56. d_curves->setLegendAttribute(QwtPlotCurve::LegendShowLine, true);
    57. d_curves->setPen(QPen(Qt::green));
    58. d_curves->setRawSamples(xval, yval, 100);
    59. d_curves->attach(plot);
    60.  
    61.  
    62. nw->resize(800,600);
    63. plot->show();
    64. nw->show();
    65. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jayalbee; 9th February 2012 at 12:36.

  4. #4
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problems with plotting straight lines with QWT

    Take a look at your Graph class.

    See what you're doing there?

    You subclass QwtPlot and create QwtPlot member variable.

    That doesn't make any sense. If you subclass QwtPlot that's all you need.
    Get rid of that myPlot variable and replace it with 'this' (which is already the plot).

    Qt Code:
    1. Graph::Graph( QWidget* parent )
    2. :
    3. QwtPlot( parent )
    4. {
    5. this->setAutoFillBackground( true );
    6. this->setPalette( QPalette( QColor( 165, 193, 228 ) ) );
    7.  
    8. this->setTitle("Plot 1.0");
    9. this->insertLegend(new QwtLegend(), QwtPlot::RightLegend);
    10.  
    11. // axes
    12. this->setAxisTitle(xBottom, "x -->" );
    13. this->setAxisScale(xBottom, 0, 10);
    14.  
    15. this->setAxisTitle(yLeft, "y -->");
    16. this->setAxisScale(yLeft, 0, 10);
    17.  
    18. this->setAutoReplot(true);
    19. }
    To copy to clipboard, switch view to plain text mode 

    Or get rid of Graph at all.
    Here's minimalistic version of what you're trying to do (if I got you right):
    Qt Code:
    1. MainWindow::MainWindow( QWidget* parent )
    2. : QMainWindow( parent )
    3. {
    4. QPushButton* pb = new QPushButton( "draw!" );
    5. connect( pb, SIGNAL( clicked() ), this, SLOT( draw() ) );
    6.  
    7. this->setCentralWidget( pb );
    8. }
    9.  
    10. void MainWindow::draw( void )
    11. {
    12. QPolygonF poly;
    13. poly << QPointF( 0, 5 ) << QPointF( 10, 5 );
    14.  
    15. QDialog d( this );
    16.  
    17. QwtPlot* plot = new QwtPlot( &d );
    18.  
    19. c->setData( poly );
    20.  
    21. c->attach( plot );
    22.  
    23. QHBoxLayout* lay = new QHBoxLayout();
    24. lay->addWidget( plot );
    25. d.setLayout( lay );
    26.  
    27. d.exec();
    28. }
    To copy to clipboard, switch view to plain text mode 

    Edit:
    Ps. It's confusing when your file name do not resemble the class name inside. Google for good coding practices.

Similar Threads

  1. QPainter::drawLine(): straight lines look aliased
    By xqrp in forum Qt Programming
    Replies: 4
    Last Post: 19th January 2012, 13:18
  2. Replies: 1
    Last Post: 29th April 2011, 12:26
  3. Replies: 0
    Last Post: 4th August 2009, 15:24
  4. Replies: 8
    Last Post: 8th April 2009, 19:51

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.