PDA

View Full Version : Read values from text file and display it in lineditbox



kulsekarr
26th June 2012, 07:12
Hi,
I am trying to read values from a text file and make it to display in the lineEdit box, i have used the code below but it is not working out.


void MainWindow::on_start_clicked()
{
QString str;
QFile file("sp.txt");
if (!file.open(QIODevice::ReadOnly))
{
QTextStream in(&file);
in >> str;
file.close();
}

ui->lineEdit->setText(str); // this is for displaying the values in the line edit box
}


i have also used this connect function in the constructor
connect(ui->start,SIGNAL(clicked()),this,SLOT(on_start_clicked ()));

start is the object name of the push button..
i have also included the header QtextStream

The text file contains values as
23
34
44

kindly do please help me out!!

aamer4yu
26th June 2012, 07:28
if (!file.open(QIODevice::ReadOnly))

There shouldn't be negation.. The if code is not executing !!

Also you can use file.readAll() to read all the text :)

sonulohani
26th June 2012, 07:33
See this code:------>

widget.h


#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include<QFile>
#include<QString>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
Q_OBJECT

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

public slots:
void updating();

private:
Ui::Widget *ui;
QFile *file;
QString str;
};

#endif // WIDGET_H


widget.cpp


#include "widget.h"
#include "ui_widget.h"
#include<QTextStream>

Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
file=new QFile("demo.txt");
if(file->open(QIODevice::ReadOnly)){
QTextStream stream(file);
str=stream.readAll();
}
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(updating()) );
}

void Widget::updating(){
ui->lineEdit->setText(str);
}

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

kulsekarr
26th June 2012, 14:53
Thank you very much for your reply ! if i run the above code it is showing exited with 0, and it is not displaying in the line edit box.

kulsekarr
27th June 2012, 06:41
Hi any one please help me out in displaying the values that are read from the text file and still it is not displaying in the line edit box. I have attached my application kindly do check it. any reply is appreciated its urgent.. please..

sonulohani
27th June 2012, 09:59
See this example ---->


I think you will now figure out....

7903

kulsekarr
27th June 2012, 11:05
Thank you for your reply but still i am facing that same problem wit the sample code that u have sent it to me help me...

sonulohani
27th June 2012, 12:26
It is working fine in my computer:-
You can see the output------>

79077908

Copy the source code and paste that in the new project.....

Added after 11 minutes:

I am making changes in your widget.zip file. Wait for some time.

Added after 6 minutes:

Hey,

Make this change in your project



ui->setupUi(this);
//QString str; //Comment this.
QFile file("sp.txt");
if(file.open(QIODevice::ReadOnly))
{
QTextStream stream(&file);
stream >> str;
file.close();
}

kulsekarr
27th June 2012, 14:00
i Have made the changes as you said but still not working....what will be problem?

high_flyer
27th June 2012, 14:25
You could help your self and those who try to help you by showing some initiative and TRY to work the problem you self, and produce as much information regarding what you have tried and what results you got.
The problem could be ANYTHING.
To reduce it from anything to just few things debugging is needed.
Therefore, debug your application.
Run it in a debugger, and step through.
Then you can answer the following questions:
1. Does the file get opened?
2. If 1 is true, is the data being read ok by the stream?
3. if 2 is true, is the slot being fired?


i Have made the changes as you said but still not working....what will be problem?
Did you notice that your 'str' variable was a local and the previous posters have changed it to a member?
Did you copy the suggested files or did you copy the code?
Make sure you work with the code you got!

sonulohani
28th June 2012, 07:04
Check This:---->

Your working project---->

7909

Ali Reza
28th June 2012, 11:17
try with

file.open(QIODevice::readonly | QIODevice::text)
and use qtextstream to exract data