Results 1 to 7 of 7

Thread: File Problem

  1. #1
    Join Date
    Sep 2011
    Posts
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Question File Problem

    Does anyone know how to read a .txt file line by line and print each line to a label by using timer?
    Label will be updated when new line comes.

    Thank you

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: File Problem

    Yes, me! What exactly is your problem?

  3. #3
    Join Date
    Sep 2011
    Posts
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: File Problem

    Im new in Qt. I have already tried many ways to solve this.

    I want to read a txt file line by line and i want to print each line with label
    For example: a. txt includes
    3.2
    4.3
    5.4
    by using timer or sleep func. a label should be updated
    1. sec -- label.text= 3.2
    2.sec --- label.text =4.3
    etc

    sorry for my bad english

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: File Problem

    So, then show us what you have so far. You wont get a ready to use code. Try to solve your problem in smaller steps and see what you really don't know. E.g. Can you read the file and store each line of text in a QStringList? etc.

  5. #5
    Join Date
    Sep 2011
    Posts
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: File Problem

    this is what i have done

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QFile>
    4. #include <QTimer>
    5. #include <QTextStream>
    6. #include <QString>
    7.  
    8.  
    9. MainWindow::MainWindow(QWidget *parent) :
    10. QMainWindow(parent),
    11. ui(new Ui::MainWindow)
    12. {
    13. ui->setupUi(this);
    14.  
    15. QTimer *timer = new QTimer(this);
    16. timer->start(2000);
    17. connect(timer,SIGNAL(timeout()),this,SLOT(DegerGetir()));
    18. }
    19.  
    20. MainWindow::~MainWindow()
    21. {
    22. delete ui;
    23. }
    24. void MainWindow:: DegerGetir()
    25. {
    26. QFile file ( "C:/Users/Onur/Desktop/Deneme2.txt" ) ;
    27. if ( !file.open ( QIODevice::ReadOnly | QIODevice::Text ))
    28. {
    29. qFatal("Dosya acilamadi!");return;
    30. }
    31. QTextStream in ( &file ) ;
    32. QString line = in.readLine () ;
    33. while ( !line.isNull ())
    34. {
    35. ui->textBrowser->append(line);
    36. line = in.readLine () ;
    37. }
    38.  
    39. file.close();
    40.  
    41. }
    To copy to clipboard, switch view to plain text mode 

    the file is opened again and again and program prints elements of file to QTextBrowser. but i want to print line by line with QLabel.

    Thank you for your interest
    Last edited by Lykurg; 11th September 2011 at 12:44. Reason: missing [code] tags

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: File Problem

    Ok, seems you know almost everything ;-) So try to do:
    - Read the file once at startup and cache it's content in a QStringList.
    - create a private variable storing the current index of the QStringList.
    - start the timer like you did and call a slot which, takes one item out of the QStringList and set this text to a label. (according to the index, and don't forget to count it)

    ..or does the content of the file change over time?

    Best.

  7. #7
    Join Date
    Sep 2011
    Posts
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: File Problem

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QFile>
    #include <QTimer>
    #include <QTextStream>
    #include <QString>
    #include <QVector>

    QVector<QString> Veriler;
    int i=0;
    int count=0;

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    DegerGetir();
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::Ekle()
    {
    //ui->textBrowser->append(line);
    if(i<count)
    {
    //ui->textBrowser->append(Veriler.at(i));
    ui->lbldeger->setText(Veriler.at(i));
    i++;
    }

    }

    void MainWindow:: DegerGetir()
    {
    QTimer *timerEkle = new QTimer(this);
    QFile file ( "C:/Users/Onur/Desktop/Deneme2.txt" ) ;
    if ( !file.open ( QIODevice::ReadOnly | QIODevice::Text ))
    {
    qFatal("Dosya acilamadi!");return;
    }

    QTextStream in ( &file ) ;
    //QString line = in.readLine () ;
    QString line;
    do
    {
    //ui->textBrowser->append(line);
    line = in.readLine () ;
    Veriler.append(line);
    count++;
    }while ( !line.isNull ());

    file.close();

    timerEkle->start(2000);
    connect(timerEkle,SIGNAL(timeout()),this,SLOT(Ekle ()));
    }




    Thank you for your help. It really helps to me. I did this and it works nice

Similar Threads

  1. problem in creating xml file.
    By Niamita in forum Qt Programming
    Replies: 1
    Last Post: 6th September 2011, 15:18
  2. Problem loading .pdf file from qrc
    By el33t in forum Newbie
    Replies: 7
    Last Post: 28th August 2011, 05:44
  3. Problem in updating XML file
    By Qt_Kid in forum Qt Programming
    Replies: 11
    Last Post: 27th February 2011, 10:54
  4. problem in qss file
    By phillip_Qt in forum Qt Programming
    Replies: 10
    Last Post: 9th April 2008, 12:36
  5. EXE File problem
    By triperzz in forum Installation and Deployment
    Replies: 8
    Last Post: 18th January 2008, 19:00

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.