PDA

View Full Version : reading a file



xaqt
9th September 2009, 08:50
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.
http://img142.imageshack.us/img142/8389/28594879.th.jpg (http://img142.imageshack.us/i/28594879.jpg/)


void read::on_readButton_clicked()
{
//?
}



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

wysota
9th September 2009, 09:09
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.

xaqt
14th September 2009, 09:28
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. :o




ui.textBrowser->setText(stream.readAll());
i writed the values to the textBrowser with this line.

jano_alex_es
14th September 2009, 09:58
maybe you can try:



ui.textBrowser->setText(stream.readAll());
QString allTheData = ui.TextBrowser.toPlainText()

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 (http://www.mayukhbose.com/freebies/c-code.php#csv). Also, remember, most of the editors use comma, but Excel uses a ";" instead :S

wysota
14th September 2009, 09:59
QString::split() gives you a list of strings.

xaqt
14th September 2009, 14:54
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.


QString value = koma.at(i);

what should i do after this line ?

Sorry for asking too many questions.:o

--------Problem solved------- :)

jano_alex_es
14th September 2009, 15:04
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()


QString stringAux = "2";
intAux = stringAux.toInt();

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