Results 1 to 6 of 6

Thread: how to use QtextEdit to open & read text files

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default how to use QtextEdit to open & read text files

    Hi, I would like the following code to open any text file and display it in a QTextEdit box. This eventually works if I give the file a file name but i want this to open any text file. This is not working and cant figure out why, please help, thx.


    void MainWindow:penfiledlg()
    {
    QFileDialog *filedlg;
    filedlg->getOpenFileName(this);

    QString fname;
    QFile file(fname);
    //QFile file("test.txt");

    file.open(QFile::ReadOnly | QFile::Text);

    QTextStream ReadFile(&file);
    ui->textEdit->setText(ReadFile.readAll());
    }

  2. #2
    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: how to use QtextEdit to open & read text files

    You are not setting your filename in to 'fname'.
    ==========================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.

  3. #3
    Join Date
    Dec 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to use QtextEdit to open & read text files

    Can you please provide an example? If I enable "QFile file("test.txt");" it will work but only open this file. I would like this code to open any text file regardless of the file name.

  4. #4
    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: how to use QtextEdit to open & read text files

    Example for what?
    I told you what you are doing wrong - just set the file name you get from the file selection dialog in to 'fname'.
    ==========================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.

Similar Threads

  1. QTextEdit partial read-only
    By pckoster in forum Qt Programming
    Replies: 1
    Last Post: 13th December 2009, 12:12
  2. Replies: 12
    Last Post: 17th June 2009, 05:34
  3. QTextEdit + paste rich text as plain text only
    By Yong in forum Qt Programming
    Replies: 2
    Last Post: 6th February 2008, 16:45
  4. How to open a file in Read Write mode
    By merry in forum Qt Programming
    Replies: 13
    Last Post: 16th November 2007, 14:40
  5. how to save sequences of text files and sound files
    By nagpalma in forum Qt Programming
    Replies: 8
    Last Post: 3rd July 2007, 00:06

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
  •  
Qt is a trademark of The Qt Company.