Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Help with setFieldWidth in QTextStream
Qt 6.11 is out! See what's new in the release blog

Help with setFieldWidth in QTextStream

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    leon9701
    wrote on last edited by
    #1

    Hey guys,

    I am writing a simple piece to output a data array with fixed width into a txt file on PC. My code is listed below:
    @
    QFile file("sf-book.txt");
    file.open(QIODevice::WriteOnly | QIODevice::Text );

    QTextStream out(&file);
    out.setIntegerBase(10);
    out.setRealNumberNotation(QTextStream::FixedNotation);
    out.setRealNumberPrecision(4);
    out.setFieldAlignment(QTextStream::AlignRight);
    out.setFieldWidth(14);
    out.setPadChar('.');

    for (int row = 0; row < 3; row++) {
    out << (float)row << 1.234 << endl;
    }
    @

    The content in the output text is like:
    ........0.0000........1.2340.............
    ........1.0000........1.2340.............
    ........2.0000........1.2340.............

    So my question is that why there are extra dots after the two data on each row? setFieldWidth also works for format characters?
    I tried enable and disable QIODevice::Text, but the result is the same.

    Thanks in advance.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      gmaro
      wrote on last edited by
      #2

      Hi,

      In your code, every "element" that you put to the out stream has field width = 14. This also applies to the endl.
      As you can see you have 13 extra dots, the 14th sign is the newline.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        leon9701
        wrote on last edited by
        #3

        Thanks for the info! :)

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved