Results 1 to 7 of 7

Thread: reading a file

  1. #1
    Join Date
    Aug 2009
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Red face reading a file

    Hi everyone;

    I'm trying to read a .txt file with a simple window design. After i manage to read the .txt file i'll try to read .csv file.

    Firstly is there a big difference between reading .txt files and .csv files. ?

    and second, I've a textBrowser and a button just like this.


    Qt Code:
    1. void read::on_readButton_clicked()
    2. {
    3. //?
    4. }
    To copy to clipboard, switch view to plain text mode 



    what should I write here ? Same way with simple c/c++ methods ?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: reading a file

    csv file is also a text file. Use QFile::readAll() to read the contents of the file and QTextBrowser::setPlainText() to set it as the contents of the text browser.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    xaqt (9th September 2009)

  4. #3
    Join Date
    Aug 2009
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: reading a file

    thank for helpful post.

    Now i can read .txt and .csv files(same as wysota said in the post). Now i need to split the comas but when it comes to add the values which i read from file to a list or an array i can not manage it.

    QList<QString> list;I created a list
    but how can i add the values from the file to the list ? After i add the values to the list i can split the values as i want. But i can not add the files to the list.



    Qt Code:
    1. ui.textBrowser->setText(stream.readAll());
    To copy to clipboard, switch view to plain text mode 
    i writed the values to the textBrowser with this line.

  5. #4
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: reading a file

    maybe you can try:

    Qt Code:
    1. ui.textBrowser->setText(stream.readAll());
    2. QString allTheData = ui.TextBrowser.toPlainText()
    To copy to clipboard, switch view to plain text mode 

    and you'll have a big QString you can split with "indexOf".

    By the way, you can use QStringList instead your list.

    But, when I had to work with CSV I use a parser. Also, remember, most of the editors use comma, but Excel uses a ";" instead :S

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: reading a file

    QString::split() gives you a list of strings.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #6
    Join Date
    Aug 2009
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: reading a file

    i did it thanks for the posts.

    after i manage to add values to my list i'm trying to convert my values string to integer because compiler dont allow me to do arithmetical operations on them.

    Qt Code:
    1. QString value = koma.at(i);
    To copy to clipboard, switch view to plain text mode 

    what should i do after this line ?

    Sorry for asking too many questions.

    --------Problem solved-------
    Last edited by xaqt; 14th September 2009 at 15:04.

  8. #7
    Join Date
    Apr 2009
    Location
    Valencia (Spain)
    Posts
    245
    Thanks
    38
    Thanked 19 Times in 19 Posts
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: reading a file

    after i manage to add values to my list i'm trying to convert my values string to integer because compiler dont allow me to do arithmetical operations on them.
    To convert from QString to integer: QString::toInt()
    Qt Code:
    1. QString stringAux = "2";
    2. intAux = stringAux.toInt();
    To copy to clipboard, switch view to plain text mode 

    just in case, to convert from integer to QString you have QString::number()

  9. The following user says thank you to jano_alex_es for this useful post:

    xaqt (14th September 2009)

Similar Threads

  1. Reading and rereading a file with QXmlStreamReader
    By TheRonin in forum Qt Programming
    Replies: 14
    Last Post: 30th April 2015, 14:04
  2. Character by Character (Unicode?) File Reading
    By mclark in forum Qt Programming
    Replies: 4
    Last Post: 22nd April 2009, 15:28
  3. help in reading XML file
    By cshiva_in in forum Qt Programming
    Replies: 1
    Last Post: 24th March 2008, 13:55
  4. QTextStream loses position while reading file
    By bjh in forum Qt Programming
    Replies: 2
    Last Post: 13th February 2008, 15:47
  5. Replies: 3
    Last Post: 18th October 2007, 18:07

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.