PDA

View Full Version : qfile read from last position



NathiyaAmose
20th July 2012, 10:15
i want to plot curve by reading 512 data from text file for each 250ms,but i can plot the curve by reading 512 data once and how can i read the text file from last read position in the file to update my plot.And my code is,


MainWin::MainWin( QWidget *parent): QwtPlot( parent ){

setAxisScale( xBottom, 0.0,512.0,50.0 );
setAutoReplot(true);
setAxisScale( yLeft, 0.0, 255.0,50.0);
canvas()->setLineWidth( 1 );
canvas()->setFrameStyle( QFrame::Box | QFrame::Plain );
canvas()->setBorderRadius( 1 );
QPalette canvasPalette;
canvasPalette.setColor( QPalette::Background, QColor( 30, 30, 50 ) );
canvasPalette.setColor( QPalette::Foreground, QColor( 133, 190, 232 ) );
canvas()->setPalette( canvasPalette );

QwtPlotCurve *d_curves=new QwtPlotCurve();
d_curves->setSymbol( new QwtSymbol( QwtSymbol::NoSymbol, Qt::NoBrush,
QPen( Qt::black ), QSize( 5, 5 ) ) );
d_curves->setPen( QPen( QColor( 200, 150, 50 ), 1.5 ) );
d_curves->setStyle( QwtPlotCurve::Lines );
d_curves->setRenderHint( QwtPlotCurve::RenderAntialiased );
d_curves->setRawSamples( xval, yval, Size );
d_curves->attach(this);
(void)startTimer(250);

}
void MainWin::timerEvent(QTimerEvent *)
{
newCurveData();
replot();

}

void MainWin::newCurveData()
{

QFile file("val/measure.txt");
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
printf("cannot open");
}
QTextStream in(&file);
int j=0;
for(j=0;j<=512;j++)
{
QString line=in.readLine();
xval[j] = j;
yval[j]=line.toDouble();

}
}

wysota
20th July 2012, 10:40
You can either reuse the same text stream object or use QTextStream::seek() and QTextStream::pos()

NathiyaAmose
21st July 2012, 07:50
thanks for your reply i will try to do it.

Added after 1 4 minutes:

now i changed the code to,

void MainWin::newCurveData()
{

QFile file("val/measure.txt");
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
printf("cannot open");
}
QTextStream in(&file);
int j=0;
in.seek(kval);
for(j=0;j<=512;j++)
{
QString line=in.readLine();
xval[j] = j;
yval[j]=line.toDouble();
kval=in.pos();

}
}
so now it s updating my plot.And could u tell me can i read from '/dev/spidev' file using this same method.

wysota
21st July 2012, 11:02
I don't see why not.

NathiyaAmose
23rd July 2012, 10:21
oh thank u