PDA

View Full Version : How to detect new line?



whoops.slo
5th January 2006, 19:27
Hi!

I try to read data from a table, each field is read as QString. One of the fields in the table has data stored in several lines (it's number is not known). How can I read that field's value line by line and stored each line in QString? I found readLine(), but I can not use it for QString stored data...

Regards,
Luka

axeljaeger
5th January 2006, 19:32
If there is any separation character between the fields, e.g. the fields are "comma seperated values", you can use QString::split to split the whole data into single chunks, each containing one field.

jacek
5th January 2006, 19:35
You need something like this:

QString str; // contains several lines of text
// ...
QStringList lines = str.split( "\n", QString::SkipEmptyParts );
foreach( QString line, lines ) {
// do something with line
}

graeme
5th January 2006, 19:59
I'd suggest that you also trim each line to remove surplus white space (extra \r for example)

foreach( QString line, lines ) {
line = line.trimmed();
}

graeme.

whoops.slo
6th January 2006, 16:49
This is the error the compiler is giving me after i had put in the document the sugested code... What went wrong?


glavni.cpp:449:34: macro "Q_FOREACH" requires 2 arguments, but only 1 given
glavni.cpp: In member function `void okno::on_o_t_izpis_clicked()':
glavni.cpp:449: error: `Q_FOREACH' undeclared (first use this function)
glavni.cpp:449: error: (Each undeclared identifier is reported only once for eac
h function it appears in.)
glavni.cpp:449: error: expected `;' before '{' token


Tnx!
Luka

jacek
6th January 2006, 17:37
What went wrong?
There should be "," instead of ";":
foreach( QString line, lines ) {
// ...
}

whoops.slo
6th January 2006, 18:02
Tnx for your help everyone! It is working now!

Regards;
Luka