PDA

View Full Version : QByteArray split out data



jeffmetal
27th May 2011, 18:37
Im trying to take data stored as a QByteArray called dldata that contains a list of newsgroups split into lines by \r\n and using spaces as a delimiter.



"215 Newsgroups in form "group high low flags"."
"alt.hipcrime.physics.research 0000000105 0000000102 y"
"alt.emircpih.physics.research 0000000050 0000000049 y"
"alt.crimehip.physics.research 0000000069 0000000065 y"
"alt.crimehip.engr.wastewater 0000000070 0000000069 y"


Im trying to split this data out to insert into a QtableWidget but i cant seem to figure it out. the code i have so far that does not work is.



QList<QByteArray> lines = dldata.split('\r\n');
foreach ( const QByteArray &line, lines)
{
qDebug() << line;
QList<QByteArray> line = line.split(" ");
foreach( const QByteArray &field, line)
{
qDebug() << field;
}
}


Could someone point me to where im going wrong.

wysota
27th May 2011, 19:13
Use double quotes instead of single quotes if you wish to use multiple characters (a string) instead of a single character (a char).

jeffmetal
27th May 2011, 19:27
I changed the single quotes to double and i got these 2 errors for that line saying



error: invalid conversion from 'const char*' to 'char'
error: initializing argument 1 of 'QList<QByteArray> QByteArray::split(char) const'


If i use single quotes they disappear but i still get the following error on the second split line.


error: 'struct QList<QByteArray>' has no member named 'split'

wysota
27th May 2011, 20:16
I changed the single quotes to double and i got these 2 errors for that line saying
The error is correct. There is no split() variant that takes a string, you may only use a single character here. Your compiler (MSVC, right?) lets this one slip but it won't work as you expect it.


If i use single quotes they disappear but i still get the following error on the second split line.
You are redeclaring a byte array variable called "line" with a list of byte arrays variable called "line". Change the variable name.

DanH
27th May 2011, 21:11
You've got two different variables named "line". It confuses me and it may be confusing you and the compiler.

jeffmetal
27th May 2011, 22:49
whoops this should have removed the duplicate line variable but it still doesnt work.


QList<QByteArray> lines = dldata.split("\r\n");
foreach ( const QByteArray &line, lines)
{
qDebug() << line;
QList<QByteArray> fields = line.split(" ");
foreach( const QByteArray &field, fields)
{
qDebug() << field;
}
}

squidge
27th May 2011, 23:18
As already stated, there's no such split function that takes a string, look at the prototype:

QList<QByteArray> QByteArray::split ( char sep ) const

jeffmetal
27th May 2011, 23:24
So how do i split out the data ?

DanH
28th May 2011, 00:57
I would recommend that you examine each byte of data, byte by byte, in a loop, and split it the way you want. Once you understand how to do that, then you'll be able to recognize how to use split(), et al, to do the job in fewer characters of code.

(I am curious as to why you're doing this with a byte array rather than a QString, since it appears to be character data.)

(You understand, of course, that a string constant is zero or more characters surrounded by double quote (") characters, while a character constant is exactly one character surrounded by single quote (') characters? If you don't know this then you need to study your basic C/C++ programming more, before you dig into Qt.)

wysota
28th May 2011, 07:24
So how do i split out the data ?

QByteArrray::indexOf()