Read values from text file after clicking a button
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
}
}
Re: Read values from text file after clicking a button
You will have to tell us what the error messages are or we will be guessing all night.
Re: Read values from text file after clicking a button
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..
Re: Read values from text file after clicking a button
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---->
Code:
#ifndef NOTEPAD_H
#define NOTEPAD_H
#include <QWidget>
#include<QFile>
#include<QTextStream>
namespace Ui {
class Notepad;
}
{
Q_OBJECT
public:
explicit Notepad
(QWidget *parent
= 0);
~Notepad();
public slots:
void showContent();
private:
Ui::Notepad *ui;
};
Code:
#include "notepad.h"
#include "ui_notepad.h"
Notepad
::Notepad(QWidget *parent
) : ui(new Ui::Notepad)
{
ui->setupUi(this);
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(showContent()));
}
void Notepad::showContent(){
QFile file("yourfile.txt");
stream>>str;
file.close();
}
ui->textEdit->setText(str);
}
Notepad::~Notepad()
{
delete ui;
}
Re: Read values from text file after clicking a button
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...
Re: Read values from text file after clicking a button
write code in between the CODE tags