PDA

View Full Version : Read values from text file after clicking a button



kulsekarr
7th June 2012, 10:11
Hi frns,
The below code is for reading values from text files and display it in the text box and i get some errors in the below code . kindly please help me out.. thanks in advance..

void MainWindow::slotTimer()
{
QFile file("Heartrate.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;

QTextStream in(&file);
while (!in.atEnd())
{
QString line = in.readLine();
TEhr(line);// displays the value of heartrate in Textbox
}

}

ChrisW67
7th June 2012, 10:15
You will have to tell us what the error messages are or we will be guessing all night.

kulsekarr
8th June 2012, 11:45
Hi,
Thanks,the error is that the variable TEhr is a textedit box and it show that it accepts only one parameter...i the last line..

sonulohani
8th June 2012, 13:05
Have you done that through designer? Could you please share your application code??? So that we would able to figure out the problem. Use signal slot for the button click, what is this slotTimer???

Added after 5 minutes:

See this code---->




#ifndef NOTEPAD_H
#define NOTEPAD_H

#include <QWidget>
#include<QFile>
#include<QTextStream>

namespace Ui {
class Notepad;
}

class Notepad : public QWidget
{
Q_OBJECT

public:
explicit Notepad(QWidget *parent = 0);
~Notepad();

public slots:
void showContent();

private:
Ui::Notepad *ui;
};






#include "notepad.h"
#include "ui_notepad.h"
Notepad::Notepad(QWidget *parent) :
QWidget(parent),
ui(new Ui::Notepad)
{
ui->setupUi(this);
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(showContent ()));
}
void Notepad::showContent(){
QString str;
QFile file("yourfile.txt");
if(file.open(QIODevice::ReadOnly)){
QTextStream stream(&file);
stream>>str;
file.close();
}
ui->textEdit->setText(str);
}

Notepad::~Notepad()
{
delete ui;
}

kulsekarr
8th June 2012, 16:55
I have pasted mycode below ...

#include "user.h"

#include <QWidget>
#include <QtCore/QDebug>
#include <QtCore/QStringList>
#include <QTextStream>
#include <QtCore>
#include <QLabel>
#include <QFile>
#include <QtGui>
#include <iostream>

User::User(QWidget *parent)
: QWidget(parent)
{

QGridLayout *layout = new QGridLayout;

lblhr = new QLabel("Heartrate");
lblsp = new QLabel("SPO2");
lblpi = new QLabel("Perfusion Index");
QFont labelFont( "TimesNewRoman", 10);

TEhr = new QTextEdit();
TEsp = new QTextEdit();
TEpi = new QTextEdit();

Bstart = new QPushButton("Start Read");
connect(Bstart, SIGNAL(clicked()),this,SLOT(slotstart()));
Bstart->setStyleSheet("* { background-color: rgb(255,125,100) }");

Bstop = new QPushButton("Stop Read");
connect(Bstop, SIGNAL(clicked()),this,SLOT(slotstop()));
Bstop->setStyleSheet("* { background-color: rgb(255,125,100) }");

Bout = new QPushButton();
connect(Bout,SIGNAL(clicked()),this,SLOT(slotout() ));


TEha = new QTextBrowser();


layout->addWidget(lblhr,0,0);
layout->addWidget(lblsp,1,0);
layout->addWidget(lblpi,2,0);
layout->addWidget(TEhr,0,2);
layout->addWidget(TEsp,1,2);
layout->addWidget(TEpi,2,2);
layout->addWidget(Bstart,0,20);
layout->addWidget(Bstop,1,20);
layout->addWidget(Bout,3,20);
layout->addWidget(TEha,5,0);



lblhr->setFont(labelFont);
lblsp->setFont(labelFont);
lblpi->setFont(labelFont);
TEhr->setFont(labelFont);
TEsp->setFont(labelFont);
TEpi->setFont(labelFont);
TEha->setFont(labelFont);



TEhr->setFixedSize(80,33);
TEsp->setFixedSize(80,33);
TEpi->setFixedSize(80,33);


// TEhr->setReadonly(true);
// TEsp->setReadOnly(true);
// TEpi->setReadOnly(true);

setLayout(layout);
}

User::~User()
{

}

void User::slotstart()
{
QString str;
QFile file("heartrate.txt");
if(file.open(QIODevice::ReadOnly))
{
QTextStream stream(&file);
stream>>str;
file.close();
}
TEhr->setText(str); ------------> To be displayed in the text box
}

void User::slotstop()
{
this->showMaximized();
}

void User::slotout()
{
this->showNormal();
}

the text files contains a random one column with some random numbers...
plz help me...

sonulohani
26th June 2012, 07:39
write code in between the CODE tags