Results 1 to 4 of 4

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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.