Results 1 to 4 of 4

Thread: String operations, printing to stdout

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default String operations, printing to stdout

    I'm not very fit with the QString class. I need to build a string representation of data which is a mixture of text and data. Is there a more elegant way than doing this?

    Qt Code:
    1. QString string;
    2. QString temp;
    3. QList<float> angles;
    4.  
    5. string = "data starts here: ";
    6. for (int i = 0; i <angles->size(); i++)
    7. {
    8. temp.sprintf("\t%.2f", angles.at(i));
    9. string.append(temp);
    10. }
    To copy to clipboard, switch view to plain text mode 

    And then what's the best way to print this string to stdout? All I know is printf from C and I can't get that to work with Qstring.

    Thanks
    Cruz

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

    Default Re: String operations, printing to stdout

    Try something like this:
    Qt Code:
    1. #include <QtDebug>
    2. //...
    3. QString string("data starts here: ");
    4. for(int i=0;i<angles->size();++i)
    5. string+=QString::number(angles.at(i), 'f', 2);
    6. qDebug() << string;
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to wysota for this useful post:

    Cruz (19th January 2009)

  4. #3
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: String operations, printing to stdout

    Qt Code:
    1. #include <iostream>
    2. ...
    3. QString sometext = "Some text ";
    4. std::cout << sometext.toLocal8Bit().constData() << 1 << " " << 1.3;
    5. ...
    To copy to clipboard, switch view to plain text mode 
    or I don't understand?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: String operations, printing to stdout

    Even
    Qt Code:
    1. qDebug() << angles;
    To copy to clipboard, switch view to plain text mode 
    works.
    J-P Nurmi

  6. The following user says thank you to jpn for this useful post:

    Cruz (20th January 2009)

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.