Read values from text file and display it in lineditbox
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_clicke d()));
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!!
Re: Read values from text file and display it in lineditbox
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 :)
Re: Read values from text file and display it in lineditbox
See this code:------>
widget.h
Code:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QFile>
#include<QString>
namespace Ui {
class Widget;
}
{
Q_OBJECT
public:
explicit Widget
(QWidget *parent
= 0);
~Widget();
public slots:
void updating();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp
Code:
#include "widget.h"
#include "ui_widget.h"
#include<QTextStream>
ui(new Ui::Widget)
{
ui->setupUi(this);
file=new QFile("demo.txt");
str=stream.readAll();
}
connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(updating()));
}
void Widget::updating(){
ui->lineEdit->setText(str);
}
Widget::~Widget()
{
delete ui;
}
Re: Read values from text file and display it in lineditbox
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.
1 Attachment(s)
Re: Read values from text file and display it in lineditbox
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..
1 Attachment(s)
Re: Read values from text file and display it in lineditbox
See this example ---->
I think you will now figure out....
Attachment 7903
Re: Read values from text file and display it in lineditbox
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...
2 Attachment(s)
Re: Read values from text file and display it in lineditbox
It is working fine in my computer:-
You can see the output------>
Attachment 7907Attachment 7908
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
Code:
ui->setupUi(this);
//QString str; //Comment this.
{
stream >> str;
file.close();
}
Re: Read values from text file and display it in lineditbox
i Have made the changes as you said but still not working....what will be problem?
Re: Read values from text file and display it in lineditbox
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?
Quote:
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!
1 Attachment(s)
Re: Read values from text file and display it in lineditbox
Check This:---->
Your working project---->
Attachment 7909
Re: Read values from text file and display it in lineditbox
try with
and use qtextstream to exract data