Results 1 to 6 of 6

Thread: loading a textfile in a qlabel

  1. #1
    Join Date
    May 2007
    Posts
    33
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default loading a textfile in a qlabel

    hi,

    can anyone tell me why the following code doesn't display the content of the given textfile in the QLabel ?

    Qt Code:
    1. QFile trainResult("26.train");
    2. QTextStream in(&trainResult);
    3.  
    4. trainLabel->setText(in.readAll());
    5. trainLabel->adjustSize();
    To copy to clipboard, switch view to plain text mode 

    26.train is a small textfile, trainLabel a simple QLabel. trainLabel->setText("abc"); works fine...

    thanks in advance
    Last edited by jacek; 11th June 2007 at 19:01. Reason: changed [qtclass] to [code]

  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: loading a textfile in a qlabel

    you didn't open the file.
    Also, the 'Qt' tags are not for code, but for reference to the docs.
    Use the code tags '#' for code.
    ==========================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
    May 2007
    Posts
    33
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: loading a textfile in a qlabel

    Quote Originally Posted by high_flyer View Post
    you didn't open the file.
    and how do i open it in this case?

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: loading a textfile in a qlabel

    Quote Originally Posted by harakiri View Post
    and how do i open it in this case?
    See the examples in QFile and QTextStream docs.
    J-P Nurmi

  5. #5
    Join Date
    May 2007
    Posts
    33
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: loading a textfile in a qlabel

    Quote Originally Posted by jpn View Post
    See the examples in QFile and QTextStream docs.
    isn't there anyone who can help me instead of posting links to the docu i've already read?

    i tried to put the file into aQString but that didn't work either:

    Qt Code:
    1. QString x = trainResult.readAll();
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: loading a textfile in a qlabel

    Quote Originally Posted by harakiri View Post
    isn't there anyone who can help me instead of posting links to the docu i've already read?
    Did you try following the links? They take you directly to the corresponding examples. Here's one of them:
    Qt Code:
    1. QFile file("in.txt");
    2. if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) // <-- you are missing this
    3. return;
    4.  
    5. QTextStream in(&file);
    6. while (!in.atEnd()) {
    7. QString line = in.readLine();
    8. process_line(line);
    9. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  7. The following user says thank you to jpn for this useful post:

    harakiri (12th June 2007)

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.