Results 1 to 1 of 1

Thread: Qwtplot: reading data from text file

  1. #1
    Join Date
    May 2013
    Location
    Melbourne
    Posts
    36
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qwtplot: reading data from text file

    Hallo all,
    I am trying to use Qwtplot to plot some data which are in a .txt file. To keep things simple, my data file is as below- the 1st column is x-data and the 2nd is y-data.

    data.txt

    1.5 1.9
    2.2 2.8
    3.1 10.2
    4.3 5.5

    When I use the following code (i.e x & y values are put straight in QPointF()), I get the plot nice and well.

    Qt Code:
    1. void Traffic::plotData()
    2. {
    3. QwtPlot *plot = new QwtPlot(QString("Curves"));
    4. this->setCentralWidget(plot);
    5.  
    6. plot->setTitle( "Plot Demo" );
    7. plot->setCanvasBackground( Qt::white );
    8. plot->setAxisScale( QwtPlot::yLeft, 0.0, 10.0);
    9. plot->insertLegend( new QwtLegend() );
    10.  
    11. QwtPlotGrid *grid = new QwtPlotGrid();
    12. grid->attach(plot );
    13.  
    14. QwtPlotCurve *curve = new QwtPlotCurve();
    15. curve->setTitle( "Pixel Count" );
    16. curve->setPen( Qt::blue, 4 ),
    17. curve->setRenderHint( QwtPlotItem::RenderAntialiased, true );
    18.  
    19. QwtSymbol *symbol = new QwtSymbol( QwtSymbol::Ellipse,
    20. QBrush( Qt::yellow ), QPen( Qt::red, 2 ), QSize( 8, 8 ) );
    21. curve->setSymbol( symbol );
    22.  
    23. QPolygonF points;
    24. points << QPointF(1.5, 1.9 ) << QPointF( 2.2, 2.8 )
    25. << QPointF( 3.1, 10.2 ) << QPointF( 4.3, 5.5 );
    26.  
    27. curve->setSamples(points);
    28. curve->attach(plot );
    29. plot->replot();
    30.  
    31. plot->resize( 600, 400 );
    32. plot->show();
    33. }
    To copy to clipboard, switch view to plain text mode 

    However, if I use the following code to get x & y values then the values are not read correctly - see the debugger output.
    Qt Code:
    1. vector<double> X, Y;
    2. double x, y;
    3.  
    4. ifstream fp("data.txt");
    5.  
    6. while (!fp.eof())
    7. {
    8. fp >> x>> y;
    9. X.push_back(x);
    10. Y.push_back(y);
    11. qDebug()<<"Hi";
    12. qDebug()<<x <<","<<y;
    13.  
    14. if (!fp)
    15. break;
    16. }
    To copy to clipboard, switch view to plain text mode 
    "Debugging starts
    &"warning: GDB: Failed to set controlling terminal: Invalid argument\n"
    Hi
    1.30547e-312 , 2.76677e-322
    Debugging has finished"

    I also tried to use QFile, QTextStream etc. but then the values read are empty.

    Any help/suggestion would be greatly appreciated (a simple example code would be great!).


    Added after 58 minutes:


    Sorry guys..I found the problem. Previously data.txt was located in pwd but when I placed in the build directory the data was read correctly.
    Last edited by GG2013; 31st May 2013 at 08:03.

Similar Threads

  1. Replies: 3
    Last Post: 8th June 2011, 07:36
  2. Reading XML file into a data file correctly?
    By falconium in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2011, 19:55
  3. Problem: Reading and editing text file data
    By dipeshtech in forum Newbie
    Replies: 2
    Last Post: 3rd May 2011, 00:47
  4. Reading data from xls file
    By addu in forum Qt Programming
    Replies: 2
    Last Post: 6th May 2010, 10:33
  5. reading data from file
    By offline in forum Qt Programming
    Replies: 1
    Last Post: 9th April 2010, 11:31

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.