PDA

View Full Version : can not plot from txt file



vanduongbk
8th August 2013, 09:38
hello every one,
i want to write a program that when i click button ,it will plot graph from a txt file
i have write a code as belows but can plot and have error ,plz help me

this is a code:



void Form::on_pushButton_clicked()
{
QFile plot;
plot.setFileName(":/history.txt");
plot.open(QFile::ReadOnly|QFile::Text);

//set plot propetis
ui->qwtPlot->setCanvasBackground(Qt::white);
ui->qwtPlot->setAxisScale(QwtPlot::xBottom,0,150);
ui->qwtPlot->setAxisScale(QwtPlot::yLeft,0,150); //max is 150

if(!curve)
curve=new QwtPlotCurve();
curve->attach(ui->qwtPlot);
QColor colour=Qt::blue;
colour.setAlpha(150);
curve->setPen(colour);

QTextStream file(&plot);

while (!file.atEnd())
{
QString line=plot.readLine();
QStringList fields=line.split(' ');
if(fields.size()>1)
{
QString xdata = fields.takeFirst();
QString ydata = fields.takeFirst();
xData.append((double)xdata.toDouble());
yData.append((double)ydata.toDouble());
}
}

//plot
curve->setSamples(&xData[0],&yData[0],xData.size());
ui->qwtPlot->replot();
}