Results 1 to 4 of 4

Thread: Read from text file

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

    Default Read from text file

    Hi,
    Can any one help me how to open a text file & read values and display it in a textbox ?

    Thanks...

  2. #2
    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 from text file

    Hey,
    I am also from Bangalore.
    So, For reading file you have to use the file handling. By using file handling, you can read the content of the text file and then set all the content on the textEdit. From that you can easily read the file. Use plainText to set the text.


    Added after 10 minutes:


    Try this code:---->
    notepad.h
    Qt Code:
    1. #ifndef NOTEPAD_H
    2. #define NOTEPAD_H
    3.  
    4. #include <QWidget>
    5. #include<QFile>
    6. #include<QTextStream>
    7.  
    8. namespace Ui {
    9. class Notepad;
    10. }
    11.  
    12. class Notepad : public QWidget
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit Notepad(QWidget *parent = 0);
    18. ~Notepad();
    19.  
    20. private:
    21. Ui::Notepad *ui;
    22. };
    23.  
    24. #endif // NOTEPAD_H
    To copy to clipboard, switch view to plain text mode 


    notepad.cpp
    Qt Code:
    1. #include "notepad.h"
    2. #include "ui_notepad.h"
    3. Notepad::Notepad(QWidget *parent) :
    4. QWidget(parent),
    5. ui(new Ui::Notepad)
    6. {
    7. ui->setupUi(this);
    8. QString str;
    9. QFile file("C:\\Users\\Sonu\\Desktop\\test.txt");
    10. if(file.open(QIODevice::ReadOnly)){
    11. QTextStream stream(&file);
    12. stream>>str;
    13. file.close();
    14. }
    15. ui->textEdit->setText(str);
    16. }
    17.  
    18. Notepad::~Notepad()
    19. {
    20. delete ui;
    21. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include "notepad.h"
    2. #include "ui_notepad.h"
    3. Notepad::Notepad(QWidget *parent) :
    4. QWidget(parent),
    5. ui(new Ui::Notepad)
    6. {
    7. ui->setupUi(this);
    8. QString str;
    9. QFile file("yourfile.txt");
    10. if(file.open(QIODevice::ReadOnly)){
    11. QTextStream stream(&file);
    12. stream>>str;
    13. file.close();
    14. }
    15. ui->textEdit->setText(str);
    16. }
    17.  
    18. Notepad::~Notepad()
    19. {
    20. delete ui;
    21. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by sonulohani; 8th June 2012 at 12:06.

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

    Default Re: Read from text file

    Hi,
    Thankyou verymuch!!! it has worked out!!

  4. #4
    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

    Wink Re: Read from text file

    notepad.ui

    demo.jpg

Similar Threads

  1. How to read the text of a pdf file?
    By Momergil in forum Newbie
    Replies: 4
    Last Post: 22nd January 2016, 10:45
  2. Read Text file using structure..
    By umulingu in forum Qt Programming
    Replies: 7
    Last Post: 14th September 2009, 11:22
  3. How to read line number in a text file
    By grsandeep85 in forum Qt Programming
    Replies: 7
    Last Post: 31st July 2009, 09:09
  4. Continuous read of text from an input file
    By ttvo in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2009, 00:09
  5. How to read text only as it is from file
    By thomasjoy in forum Qt Programming
    Replies: 3
    Last Post: 9th August 2007, 08: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.