Results 1 to 1 of 1

Thread: QListWidget first line in file not read

  1. #1
    Join Date
    Apr 2020
    Posts
    14
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QListWidget first line in file not read

    Hallo,

    my program reads an txt file to a QListWidget, but the first row in the txt file is not shown when i open it. All the other rows are shown, except line 1.

    Also you can see that there is an empty line after last filled row. in the txt there is no line or space or tab after "9".

    Qt Code:
    1. void MainWindow::on_btnRead_clicked()
    2. {
    3. QFile file(pfad);
    4. if(!file.open(QFile::ReadOnly | QFile::Text)){
    5. QMessageBox::warning(this,"..","keine datei gefunden");
    6. return;
    7. }
    8. QTextStream in(&file);
    9. QString text = in.readLine();
    10.  
    11. while (!text.isNull()) {
    12. text = in.readLine();
    13. ui->listWidget->addItem(text);
    14. }
    15.  
    16. file.close();
    17. }
    To copy to clipboard, switch view to plain text mode 

    txt:

    Auswahl_017.png

    output in program:

    Auswahl_018.png


    Added after 1 50 minutes:


    Code corrected, now it works:

    Qt Code:
    1. void MainWindow::on_btnRead_clicked()
    2. {
    3. QFile file(pfad);
    4. if(!file.open(QFile::ReadOnly | QFile::Text)){
    5. QMessageBox::warning(this,"..","keine datei gefunden");
    6. return;
    7. }
    8.  
    9. QTextStream in(&file);
    10. QString text;
    11.  
    12. while (!in.atEnd()) {
    13. text = in.readLine();
    14. ui->listWidget->addItem(text);
    15. }
    16.  
    17. file.close();
    18. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by vitalic; 21st April 2020 at 17:37.

Similar Threads

  1. Read a file after a specific line
    By kahlenberg in forum Qt Programming
    Replies: 4
    Last Post: 20th September 2018, 19:35
  2. Replies: 1
    Last Post: 18th January 2012, 10:28
  3. Read gzip file line-by-line
    By The_Fallen in forum Qt Programming
    Replies: 4
    Last Post: 6th September 2011, 14:41
  4. Read a specific line from a file
    By rleojoseph in forum Qt Programming
    Replies: 11
    Last Post: 21st March 2011, 12:58
  5. How to read line from file
    By Krishnacins in forum Newbie
    Replies: 10
    Last Post: 2nd June 2006, 00:14

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.