Results 1 to 4 of 4

Thread: QwtPlot + UI problem

  1. #1
    Join Date
    Apr 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QwtPlot + UI problem

    Hello, I'm trying to learn how to use Qwt and I'm stucked with one problem.

    When I create a custom application with just main function everything works fine. (the code is based on examples from qwt)

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4.  
    5. double x[100];
    6. double y[100];
    7.  
    8. for(int i=0;i<100;i++)
    9. {
    10. x[i] = i/20.0;
    11. y[i] = sin(x[i]);
    12. }
    13. QHBoxLayout *layout = new QHBoxLayout(&w);
    14. layout->setContentsMargins( 0, 0, 0, 0 );
    15. QwtPlot *myPlot = new QwtPlot(&w);
    16. myPlot->setMinimumSize(w.geometry().width()-20,w.geometry().height()-20);
    17. // add curve
    18. QwtPlotCurve *curve = new QwtPlotCurve();
    19. curve->setRawSamples(x, y, 100);
    20. curve->setSymbol(new QwtSymbol(QwtSymbol::Cross, Qt::NoBrush,
    21. QPen(Qt::black), QSize(5, 5) ) );
    22. curve->setPen(QColor(Qt::darkGreen));
    23. curve->setStyle(QwtPlotCurve::Lines);
    24. curve->setCurveAttribute(QwtPlotCurve::Fitted);
    25. curve->attach(myPlot);
    26. QPushButton *b = new QPushButton(&w);
    27. layout->addWidget(myPlot);
    28. layout->addWidget(b);
    29. w.setLayout(layout);
    30. w.show();
    31.  
    32. return a.exec();
    33. }
    To copy to clipboard, switch view to plain text mode 

    The problem is when i try to copy this part of code and put it in MainWindow designed in QtCreator.

    Qt Code:
    1. void MainWindow::initGraph()
    2. {
    3. double x[100];
    4. double y[100];
    5.  
    6. for(int i=0;i<100;i++)
    7. {
    8. x[i] = i/100.0;
    9. y[i] = sin(x[i]);
    10. }
    11. QHBoxLayout *layout = new QHBoxLayout(ui->widget);
    12. layout->setContentsMargins( 0, 0, 0, 0 );
    13. QwtPlot *myPlot = new QwtPlot(ui->widget);
    14. myPlot->setMinimumSize(ui->widget->geometry().width()-20,ui->widget->geometry().height()-20);
    15. myPlot->updateGeometry();
    16. // add curves
    17. QwtPlotCurve *curve = new QwtPlotCurve();
    18. curve->setRawSamples(x, y, 100);
    19. curve->setSymbol(new QwtSymbol(QwtSymbol::Cross, Qt::NoBrush,
    20. QPen(Qt::black), QSize(5, 5) ) );
    21. curve->setPen(QColor(Qt::darkGreen));
    22. curve->setStyle(QwtPlotCurve::Lines);
    23. curve->setCurveAttribute(QwtPlotCurve::Fitted);
    24. curve->attach(myPlot);
    25. myPlot->autoReplot();
    26. myPlot->replot();
    27. myPlot->autoRefresh();
    28. layout->addWidget(myPlot);
    29. ui->widget->setLayout(layout);
    30. }
    31.  
    32. //and main
    33.  
    34. int main(int argc, char *argv[])
    35. {
    36. QApplication a(argc, argv);
    37. MainWindow w;
    38. w.initGraph();
    39. w.show();
    40.  
    41. return a.exec();
    42. }
    To copy to clipboard, switch view to plain text mode 

    After running code above I see my app, axis of the plot are scaled but curve is not painted properly (I think only first point is painted). I tried couple of functions from plot to fix it (as you can see in code above). I've created 2 or 3 gui apps using qtCreator's designer and even with just 1 main widget it doesn't work fine. Could anyone tell me where do I make a mistake and how to fix that problem?

    Sorry if someone finds it hard to understand what I've written here but english is not my native language.

  2. #2
    Join Date
    Nov 2007
    Posts
    55
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QwtPlot + UI problem

    1. you must first define the constructor of your MainWindow class, and don't forget to derive it from QMainWindow.
    2. The way your programm is structured, you do not need an Mainwindow::initGraph() method, you can put everything in your MainWindow() method.
    3. I also recommend to split the code in 3 files, main.cpp, mainwindow.cpp and mainwindow.h

  3. #3
    Join Date
    Apr 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QwtPlot + UI problem

    [EDIT] : hmn maybe I didn't say all of this correct (and coping just a fragments of bigger code is not a good thing) so this is simple new project with my problem...

    mainwindow.h :

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17.  
    18. private:
    19. Ui::MainWindow *ui;
    20. };
    21.  
    22. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include <qwt_plot_curve.h>
    2. #include <qwt_plot.h>
    3. #include <qapplication.h>
    4. #include <qwt_symbol.h>
    5. #include <qwt_math.h>
    6. #include <qcolor.h>
    7. #include <qlayout.h>
    8. #include <qpainter.h>
    9. #include "mainwindow.h"
    10. #include "ui_mainwindow.h"
    11.  
    12. MainWindow::MainWindow(QWidget *parent) :
    13. QMainWindow(parent),
    14. ui(new Ui::MainWindow)
    15. {
    16. ui->setupUi(this);
    17. double x[100];
    18. double y[100];
    19.  
    20. for(int i=0;i<100;i++)
    21. {
    22. x[i] = i/20.0;
    23. y[i] = 0;
    24. }
    25. QwtPlot *myPlot = new QwtPlot(this);
    26. // add curves
    27. QwtPlotCurve *curve = new QwtPlotCurve();
    28. curve->setRawSamples(x, y, 100);
    29. curve->setSymbol(new QwtSymbol(QwtSymbol::Cross, Qt::NoBrush,
    30. QPen(Qt::black), QSize(5, 5) ) );
    31. curve->setPen(QColor(Qt::darkGreen));
    32. curve->setStyle(QwtPlotCurve::Lines);
    33. curve->setCurveAttribute(QwtPlotCurve::Fitted);
    34. curve->attach(myPlot);
    35. this->setCentralWidget(myPlot);
    36. myPlot->autoReplot();
    37. myPlot->replot();
    38. }
    39.  
    40. MainWindow::~MainWindow()
    41. {
    42. delete ui;
    43. }
    To copy to clipboard, switch view to plain text mode 


    main.cpp
    Qt Code:
    1. #include <qwt_plot_curve.h>
    2. #include <qwt_plot.h>
    3. #include <QApplication>
    4. #include <qwt_symbol.h>
    5. #include <qwt_math.h>
    6. #include <qcolor.h>
    7. #include <QDebug>
    8. #include <QPushButton>
    9. #include <qlayout.h>
    10. #include <qpainter.h>
    11. #include "mainwindow.h"
    12.  
    13. int main(int argc, char *argv[])
    14. {
    15. QApplication a(argc, argv);
    16. /*
    17.   QWidget w;
    18.   double x[100];
    19.   double y[100];
    20.  
    21.   for(int i=0;i<100;i++)
    22.   {
    23.   x[i] = i/20.0;
    24.   y[i] = sin(x[i]);
    25.   }
    26.   QHBoxLayout *layout = new QHBoxLayout(&w);
    27.   layout->setContentsMargins( 0, 0, 0, 0 );
    28.   QwtPlot *myPlot = new QwtPlot(&w);
    29.   myPlot->setMinimumSize(w.geometry().width()-20,w.geometry().height()-20);
    30.   myPlot->updateGeometry();
    31.   // add curves
    32.   QwtPlotCurve *curve = new QwtPlotCurve();
    33.   curve->setRawSamples(x, y, 100);
    34.   curve->setSymbol(new QwtSymbol(QwtSymbol::Cross, Qt::NoBrush,
    35.   QPen(Qt::black), QSize(5, 5) ) );
    36.   curve->setPen(QColor(Qt::darkGreen));
    37.   curve->setStyle(QwtPlotCurve::Lines);
    38.   curve->setCurveAttribute(QwtPlotCurve::Fitted);
    39.   curve->attach(myPlot);
    40.   layout->addWidget(myPlot);
    41.   w.update();
    42. */
    43. MainWindow w;
    44. w.show();
    45.  
    46. return a.exec();
    47. }
    To copy to clipboard, switch view to plain text mode 


    mainwindow.ui
    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <ui version="4.0">
    3. <class>MainWindow</class>
    4. <widget class="QMainWindow" name="MainWindow">
    5. <property name="geometry">
    6. <rect>
    7. <x>0</x>
    8. <y>0</y>
    9. <width>400</width>
    10. <height>300</height>
    11. </rect>
    12. </property>
    13. <property name="windowTitle">
    14. <string>MainWindow</string>
    15. </property>
    16. <widget class="QWidget" name="centralWidget"/>
    17. </widget>
    18. <layoutdefault spacing="6" margin="11"/>
    19. <resources/>
    20. <connections/>
    21. </ui>
    To copy to clipboard, switch view to plain text mode 

    two outcomes (on screens you can see which part of main function was commented which not)



    and second



    the question is : why first outcome is like that ? it paints only first point where is the rest ?
    Last edited by petromp; 5th April 2012 at 11:51.

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

    Default Re: QwtPlot + UI problem

    Your code uses QwtPlotCurve::setRawSamples(), but the passed arrays are from the stack and gone after leaving the constructor.

    Uwe

Similar Threads

  1. Replies: 0
    Last Post: 17th February 2012, 08:41
  2. QWTPLOT problem
    By umulingu in forum Qwt
    Replies: 5
    Last Post: 25th August 2009, 16:34
  3. Replies: 6
    Last Post: 14th May 2009, 12:02
  4. Problem regarding QWTPLOT
    By sudheer168 in forum Qwt
    Replies: 1
    Last Post: 6th January 2009, 14:18
  5. Problem with QwtPlot
    By sudheer168 in forum Qwt
    Replies: 1
    Last Post: 11th September 2008, 09:04

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.