Results 1 to 10 of 10

Thread: QByteArray split out data

  1. #1
    Join Date
    Jul 2007
    Posts
    11
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QByteArray split out data

    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.

    Qt Code:
    1. QList<QByteArray> lines = dldata.split('\r\n');
    2. foreach ( const QByteArray &line, lines)
    3. {
    4. qDebug() << line;
    5. QList<QByteArray> line = line.split(" ");
    6. foreach( const QByteArray &field, line)
    7. {
    8. qDebug() << field;
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    Could someone point me to where im going wrong.

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

    Default Re: QByteArray split out data

    Use double quotes instead of single quotes if you wish to use multiple characters (a string) instead of a single character (a char).
    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. #3
    Join Date
    Jul 2007
    Posts
    11
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QByteArray split out data

    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'

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

    Default Re: QByteArray split out data

    Quote Originally Posted by jeffmetal View Post
    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.
    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.


  5. #5
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: QByteArray split out data

    You've got two different variables named "line". It confuses me and it may be confusing you and the compiler.

  6. #6
    Join Date
    Jul 2007
    Posts
    11
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QByteArray split out data

    whoops this should have removed the duplicate line variable but it still doesnt work.

    Qt Code:
    1. QList<QByteArray> lines = dldata.split("\r\n");
    2. foreach ( const QByteArray &line, lines)
    3. {
    4. qDebug() << line;
    5. QList<QByteArray> fields = line.split(" ");
    6. foreach( const QByteArray &field, fields)
    7. {
    8. qDebug() << field;
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QByteArray split out data

    As already stated, there's no such split function that takes a string, look at the prototype:

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

  8. #8
    Join Date
    Jul 2007
    Posts
    11
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QByteArray split out data

    So how do i split out the data ?

  9. #9
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: QByteArray split out data

    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.)

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

    Default Re: QByteArray split out data

    Quote Originally Posted by jeffmetal View Post
    So how do i split out the data ?
    QByteArrray::indexOf()
    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.


Similar Threads

  1. Replies: 7
    Last Post: 6th February 2017, 20:10
  2. QByteArray split to a list
    By ruben.rodrigues in forum Qt Programming
    Replies: 2
    Last Post: 1st November 2010, 11:49
  3. split QByteArray
    By xproch13 in forum Newbie
    Replies: 2
    Last Post: 29th October 2010, 22:50
  4. inver data from QByteArray
    By aj2903 in forum Qt Programming
    Replies: 2
    Last Post: 16th January 2010, 08:56
  5. QByteArray with network data
    By merlvingian in forum Qt Programming
    Replies: 1
    Last Post: 1st June 2007, 18:53

Tags for this Thread

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.