Results 1 to 4 of 4

Thread: How to use QTextStream to replace std::ostringstream

  1. #1
    Join Date
    Nov 2020
    Location
    Syracuse NY
    Posts
    4
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default How to use QTextStream to replace std::ostringstream

    I have the following code that I am trying to add a double number to a QLabel that must have the following format MMM.NN , this a number less than 100.0 should have a leading 0

    Qt Code:
    1. double _Heading = 35.5;
    2.  
    3. QString _TextForDisplay;
    4. QTextStream _Stream(&_TextForDisplay);
    5. _Stream.setRealNumberNotation(QTextStream::FixedNotation);
    6. _Stream.setRealNumberPrecision(2);
    7. _Stream.setFieldAlignment(QTextStream::AlignRight);
    8. _Stream.setPadChar('0');
    9.  
    10. _Stream << "HDG: " << QTextStream::ForcePoint << _Heading;
    11.  
    12. QString _qs = *(lcStream.string());
    13. std::cout << "_Stream: " << _qs.toUtf8().constData() << std::endl;
    To copy to clipboard, switch view to plain text mode 

    But I get

    "_Stream : 235.50" not "035.50".

    ??? Where is the leading "2" coming from???

    >>

    Note that using std:stringstream produces the expected results:

    Qt Code:
    1. std::ostringstream _TextStream;
    2. _TextStream << "HDG: " << std::dec << std::fixed << std::setprecision(2) << _Heading;
    3. std::cout << lcTextStream.str() << std::endl;
    To copy to clipboard, switch view to plain text mode 

    "035.50"

    Thanks.
    Last edited by d_stranz; 28th November 2020 at 19:01. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to use QTextStream to replace std::ostringstream

    In line 12, you are retrieving the contents of a variable named "lcStream", which is not the same variable as "_Stream".
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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

    edmhourigan (29th November 2020)

  4. #3
    Join Date
    Nov 2020
    Location
    Syracuse NY
    Posts
    4
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: How to use QTextStream to replace std::ostringstream

    That's simply typo on line 12. Thanks.


    Added after 5 minutes:


    Corrected:
    --------------
    Qt Code:
    1. double _Heading = 35.5;
    2.  
    3. QString _TextForDisplay;
    4. QTextStream _Stream(&_TextForDisplay);
    5. _Stream.setRealNumberNotation(QTextStream::FixedNotation);
    6. _Stream.setRealNumberPrecision(2);
    7. _Stream.setFieldAlignment(QTextStream::AlignRight);
    8. _Stream.setPadChar('0');
    9.  
    10. _Stream << "HDG: " << QTextStream::ForcePoint << _Heading;
    11.  
    12. QString _qs = *(_Stream.string());
    13. std::cout << "_Stream: " << _qs.toUtf8().constData() << std::endl;
    To copy to clipboard, switch view to plain text mode 

    Is the '2' prefix coming from the call on line 6?
    Qt Code:
    1. _Stream.setRealNumberPrecision(2);
    To copy to clipboard, switch view to plain text mode 

    If so, that seems like a bug in QTextStream, right?


    Added after 14 minutes:


    The problem was caused by adding this:
    '<< QTextStream::ForcePoint'

    ForcePoint operator forces comma separators, not decimal point (poorly named operator, IMHO).


    Changing to
    Qt Code:
    1. _Stream << "HDG: " << _Heading;
    To copy to clipboard, switch view to plain text mode 

    Fixes the leading '2' digit.

    >>> IS THIS A BUG in QTextStream ??? <<<
    Last edited by edmhourigan; 29th November 2020 at 14:03.

  5. #4
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use QTextStream to replace std::ostringstream

    Why should this be a bug? Out tell QTextStream to output 'QTextStream::ForcePoint' which is 2. What you want is QTextStream::forcePoint().

Similar Threads

  1. search and replace in js with qt
    By Same_Andy in forum Newbie
    Replies: 1
    Last Post: 30th January 2016, 01:54
  2. Problems with replace
    By feraudyh in forum Qt Tools
    Replies: 1
    Last Post: 27th March 2014, 08:54
  3. Replace keyPressEvent (dot with tab)
    By the_bis in forum Qt Programming
    Replies: 1
    Last Post: 6th June 2008, 11:43
  4. How do I replace a widget?
    By pir in forum Qt Programming
    Replies: 4
    Last Post: 24th July 2006, 17:51

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.