Results 1 to 4 of 4

Thread: Qwt/Qt read data from an external file and plot them

  1. #1
    Join Date
    Jun 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Qwt/Qt read data from an external file and plot them

    Hi,
    I want to edit the example Scatterplot that comes with Qwt in such a way that i can read data(x and y coordinates) from an external txt file and plot it like the example. I am new in Qt/Qwt/C++, but I learned C# at school.
    I know I need QTextStream to read the data but I dont now how to save it in 2 arrays(x and y) and the next problem is I dont now what I need/I can delete from the source code of the example. I'm sorry for my bad english but I need your help D:

    mainwindow.cpp:

    #include "mainwindow.h"
    #include "plot.h"
    #include <qmath.h>

    static double randomValue()
    {
    // a number between [ 0.0, 1.0 ]
    return ( qrand() % 100000 ) / 100000.0;
    }

    MainWindow::MainWindow()
    {
    d_plot = new Plot( this );
    d_plot->setTitle( "Scatter Plot" );
    setCentralWidget( d_plot );

    // a million points
    setSamples( 100000 );
    }

    void MainWindow::setSamples( int numPoints )
    {
    QPolygonF samples;

    for ( int i = 0; i < numPoints; i++ )
    {
    const double x = randomValue() * 24.0 + 1.0;
    const double y = ::log( 10.0 * ( x - 1.0 ) + 1.0 )
    * ( randomValue() * 0.5 + 0.9 );

    samples += QPointF( x, y );
    }

    d_plot->setSamples( samples );
    }


    plot.cpp:

    #include "plot.h"
    #include <qwt_plot_magnifier.h>
    #include <qwt_plot_panner.h>
    #include <qwt_plot_picker.h>
    #include <qwt_picker_machine.h>
    #include <qwt_plot_curve.h>

    class DistancePicker: public QwtPlotPicker
    {
    public:
    DistancePicker( QWidget *canvas ):
    QwtPlotPicker( canvas )
    {
    setTrackerMode( QwtPicker::ActiveOnly );
    setStateMachine( new QwtPickerDragLineMachine() );
    setRubberBand( QwtPlotPicker::PolygonRubberBand );
    }

    virtual QwtText trackerTextF( const QPointF &pos ) const
    {
    QwtText text;

    const QPolygon points = selection();
    if ( !points.isEmpty() )
    {
    QString num;
    num.setNum( QLineF( pos, invTransform( points[0] ) ).length() );

    QColor bg( Qt::white );
    bg.setAlpha( 200 );

    text.setBackgroundBrush( QBrush( bg ) );
    text.setText( num );
    }
    return text;
    }
    };

    Plot::Plot( QWidget *parent ):
    QwtPlot( parent ),
    d_curve( NULL )
    {
    canvas()->setStyleSheet(
    "border: 2px solid Black;"
    "border-radius: 15px;"
    "background-color: qlineargradient( x1: 0, y1: 0, x2: 0, y2: 1,"
    "stop: 0 LemonChiffon, stop: 1 PaleGoldenrod );"
    );

    // attach curve
    d_curve = new QwtPlotCurve( "Scattered Points" );
    d_curve->setPen( QColor( "Purple" ) );

    // when using QwtPlotCurve::ImageBuffer simple dots can be
    // rendered in parallel on multicore systems.
    d_curve->setRenderThreadCount( 0 ); // 0: use QThread::idealThreadCount()

    d_curve->attach( this );

    setSymbol( NULL );

    // panning with the left mouse button
    (void )new QwtPlotPanner( canvas() );

    // zoom in/out with the wheel
    QwtPlotMagnifier *magnifier = new QwtPlotMagnifier( canvas() );
    magnifier->setMouseButton( Qt::NoButton );

    // distanve measurement with the right mouse button
    DistancePicker *picker = new DistancePicker( canvas() );
    picker->setMousePattern( QwtPlotPicker::MouseSelect1, Qt::RightButton );
    picker->setRubberBandPen( QPen( Qt::blue ) );
    }

    void Plot::setSymbol( QwtSymbol *symbol )
    {
    d_curve->setSymbol( symbol );

    if ( symbol == NULL )
    {
    d_curve->setStyle( QwtPlotCurve::Dots );
    }
    }

    void Plot::setSamples( const QVector<QPointF> &samples )
    {
    d_curve->setPaintAttribute(
    QwtPlotCurve::ImageBuffer, samples.size() > 1000 );

    d_curve->setSamples( samples );
    }

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,277
    Thanked 871 Times in 819 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qwt/Qt read data from an external file and plot them

    In your case you have to replace MainWindow::setSamples:

    Qt Code:
    1. void MainWindow::setSamples( )
    2. {
    3. QPolygonF samples;
    4.  
    5. // read your points from file and append them to samples
    6.  
    7. d_plot->setSamples( samples );
    8. }
    To copy to clipboard, switch view to plain text mode 
    Uwe

  3. #3
    Join Date
    Jun 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Qwt/Qt read data from an external file and plot them

    Ty Uwe

    #include "mainwindow.h"
    #include "plot.h"
    #include <qmath.h>
    #include <qtextstream.h>
    #include <QtCore>
    #include <QDebug>


    MainWindow::MainWindow()
    {
    d_plot = new Plot( this );
    d_plot->setTitle( "Scatter Plot" );
    setCentralWidget( d_plot );
    bool a=true;
    QPolygonF samples;
    setSamples( 10 );

    QFile file ("C:/koordinaten/koordinaten.txt");
    if(file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
    QTextStream stream(&file);
    QString line;
    do
    {
    line=stream.readLine();
    if(a)
    {
    double x=line.toDouble();
    a=false;
    }else
    {
    double y=line.toDouble();
    a=true;
    samples << QPointF( x, y );
    }
    }while(!line.isNull());

    file.close();
    d_plot->setSamples( samples );

    }
    }

    I want to read the file koordinaten.txt line 1 = x1, line 2 = y1, line 3 = x2, line 4 = y2....

    and i get this issues:
    C:\QwtSource\qwt-6.1.3\examples\scatterplot\mainwindow.cpp:-1: In constructor 'MainWindow::MainWindow()':
    C:\QwtSource\qwt-6.1.3\examples\scatterplot\mainwindow.cpp:174: warning: unused variable 'x' [-Wunused-variable]
    double x=line.toDouble();
    ^
    C:\QwtSource\qwt-6.1.3\examples\scatterplot\mainwindow.cpp:180: error: no matching function for call to 'QPointF::QPointF(<unresolved overloaded function type>, double&)'
    samples << QPointF( x, y );
    ^

    can anyone explain it to me where are the issues and what happend.

    later I want to edit it in such a way that I can read the dokument like: line1= 2;3 ,line2= 6;9 ...... x1=2,y1=3,x2=6,y2=9

    Edit: I found all the issues . Now I only had to edit it in such a way that I can read the dokument like: line1= 2;3 ,line2= 6;9 ...... x1=2,y1=3,x2=6,y2=9
    Last edited by kore; 22nd June 2016 at 11:25.

  4. #4
    Join Date
    Jun 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Qwt/Qt read data from an external file and plot them

    Now I had edit the example Scatterplot that comes with Qwt in such a way that i can read data(x and y coordinates) from an external CSV file and plot it like the example.
    But now i want to select the properties (x,y,color) over 3 Comboboxes and my problem is that this example (Scatterplot) dont had a folder named Forms or a .ui file.
    Now my questions:
    Is it possible?
    How or where i had to put the QComboBox syntax? because there dont exist an .ui file
    Exists a command with which I can create a new window/ui in which appear the combo boxes ?

    ty for all answers^^

Similar Threads

  1. Replies: 0
    Last Post: 1st August 2014, 14:43
  2. qwt plot to scroll data text file
    By vanduongbk in forum Qwt
    Replies: 0
    Last Post: 1st November 2013, 04:18
  3. Replies: 0
    Last Post: 13th February 2013, 15:25
  4. How Can i Load data from file.ecg and plot it ?
    By ahmed ali in forum Qt Programming
    Replies: 27
    Last Post: 27th March 2012, 17:37
  5. Replies: 6
    Last Post: 26th March 2009, 05:45

Tags for this Thread

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.