Results 1 to 7 of 7

Thread: How to detect new line?

  1. #1
    Join Date
    Jan 2006
    Location
    Kranj, Slovenia
    Posts
    34
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question How to detect new line?

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: How to detect new line?

    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.
    It's nice to be important but it's more important to be nice.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to detect new line?

    You need something like this:
    Qt Code:
    1. QString str; // contains several lines of text
    2. // ...
    3. QStringList lines = str.split( "\n", QString::SkipEmptyParts );
    4. foreach( QString line, lines ) {
    5. // do something with line
    6. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 6th January 2006 at 17:35. Reason: fixed typo

  4. #4
    Join Date
    Jan 2006
    Posts
    40
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to detect new line?

    I'd suggest that you also trim each line to remove surplus white space (extra \r for example)
    Qt Code:
    1. foreach( QString line, lines ) {
    2. line = line.trimmed();
    3. }
    To copy to clipboard, switch view to plain text mode 

    graeme.
    Last edited by graeme; 7th January 2006 at 00:21. Reason: fix typo...

  5. #5
    Join Date
    Jan 2006
    Location
    Kranj, Slovenia
    Posts
    34
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to detect new line?

    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:n_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

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to detect new line?

    Quote Originally Posted by whoops.slo
    What went wrong?
    There should be "," instead of ";":
    Qt Code:
    1. foreach( QString line, lines ) {
    2. // ...
    3. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Kranj, Slovenia
    Posts
    34
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to detect new line?

    Tnx for your help everyone! It is working now!

    Regards;
    Luka

Similar Threads

  1. QTcpSocket exception.
    By Fastman in forum Qt Programming
    Replies: 9
    Last Post: 29th January 2008, 14:51
  2. Some very weird compilation warnings
    By MarkoSan in forum Qt Programming
    Replies: 21
    Last Post: 23rd January 2008, 17:48
  3. Qwizard crashed when created in a slot
    By joshlareau in forum Qt Programming
    Replies: 9
    Last Post: 15th January 2008, 10:16
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 07:13
  5. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 19:42

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.