Results 1 to 12 of 12

Thread: Read values from text file and display it in lineditbox

  1. #1
    Join Date
    Dec 2011
    Location
    Bangalore,India
    Posts
    38
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default 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:n_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!!

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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

  3. The following user says thank you to aamer4yu for this useful post:

    kulsekarr (26th June 2012)

  4. #3
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Read values from text file and display it in lineditbox

    See this code:------>

    widget.h
    Qt Code:
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3.  
    4. #include <QWidget>
    5. #include<QFile>
    6. #include<QString>
    7.  
    8. namespace Ui {
    9. class Widget;
    10. }
    11.  
    12. class Widget : public QWidget
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit Widget(QWidget *parent = 0);
    18. ~Widget();
    19.  
    20. public slots:
    21. void updating();
    22.  
    23. private:
    24. Ui::Widget *ui;
    25. QFile *file;
    26. QString str;
    27. };
    28.  
    29. #endif // WIDGET_H
    To copy to clipboard, switch view to plain text mode 

    widget.cpp
    Qt Code:
    1. #include "widget.h"
    2. #include "ui_widget.h"
    3. #include<QTextStream>
    4.  
    5. Widget::Widget(QWidget *parent) :
    6. QWidget(parent),
    7. ui(new Ui::Widget)
    8. {
    9. ui->setupUi(this);
    10. file=new QFile("demo.txt");
    11. if(file->open(QIODevice::ReadOnly)){
    12. QTextStream stream(file);
    13. str=stream.readAll();
    14. }
    15. connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(updating()));
    16. }
    17.  
    18. void Widget::updating(){
    19. ui->lineEdit->setText(str);
    20. }
    21.  
    22. Widget::~Widget()
    23. {
    24. delete ui;
    25. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to sonulohani for this useful post:

    kulsekarr (26th June 2012)

  6. #4
    Join Date
    Dec 2011
    Location
    Bangalore,India
    Posts
    38
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.
    Last edited by kulsekarr; 26th June 2012 at 16:05. Reason: updated contents

  7. #5
    Join Date
    Dec 2011
    Location
    Bangalore,India
    Posts
    38
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default 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..
    Attached Files Attached Files

  8. #6
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Read values from text file and display it in lineditbox

    See this example ---->


    I think you will now figure out....

    untitled1.zip
    Last edited by sonulohani; 27th June 2012 at 10:59.

  9. The following user says thank you to sonulohani for this useful post:

    kulsekarr (27th June 2012)

  10. #7
    Join Date
    Dec 2011
    Location
    Bangalore,India
    Posts
    38
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default 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...

  11. #8
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Cool Re: Read values from text file and display it in lineditbox

    It is working fine in my computer:-
    You can see the output------>

    Capture.JPGCapture1.JPG

    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

    Qt Code:
    1. ui->setupUi(this);
    2. //QString str; //Comment this.
    3. QFile file("sp.txt");
    4. if(file.open(QIODevice::ReadOnly))
    5. {
    6. QTextStream stream(&file);
    7. stream >> str;
    8. file.close();
    9. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by sonulohani; 27th June 2012 at 13:26.

  12. The following user says thank you to sonulohani for this useful post:

    kulsekarr (27th June 2012)

  13. #9
    Join Date
    Dec 2011
    Location
    Bangalore,India
    Posts
    38
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default 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?

  14. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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?

    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!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  15. The following user says thank you to high_flyer for this useful post:

    kulsekarr (28th June 2012)

  16. #11
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Cool Re: Read values from text file and display it in lineditbox

    Check This:---->

    Your working project---->

    Widget(1).zip

  17. The following user says thank you to sonulohani for this useful post:

    kulsekarr (28th June 2012)

  18. #12
    Join Date
    Jun 2012
    Location
    Iran , Tehran
    Posts
    93
    Thanks
    5
    Platforms
    Unix/X11 Windows Android

    Default Re: Read values from text file and display it in lineditbox

    try with
    Qt Code:
    1. file.open(QIODevice::readonly | QIODevice::text)
    To copy to clipboard, switch view to plain text mode 
    and use qtextstream to exract data
    Life is about making the right decisions and moving on.

Similar Threads

  1. Replies: 5
    Last Post: 26th June 2012, 08:39
  2. How to display text from a file to a text browser
    By ironmantis7x in forum Newbie
    Replies: 11
    Last Post: 14th June 2012, 16:23
  3. Replies: 3
    Last Post: 17th March 2010, 11:31
  4. How to read rtf file and display it?
    By sunnysun520 in forum Qt Programming
    Replies: 2
    Last Post: 16th May 2009, 15:48
  5. How to read text only as it is from file
    By thomasjoy in forum Qt Programming
    Replies: 3
    Last Post: 9th August 2007, 09:47

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.