roundtheworld
roundtheworld

Reputation: 2795

QTextStream formatting issue

I am trying to use a QTextStream to format numerical output. I've simplified my issue into a little test program to demonstrate my problem. Following is my code:

#include <QCoreApplication>
#include <QTextStream>
#include <QFile>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QFile file("testfile.txt");

    if (file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        QTextStream out(&file);

        out.setFieldAlignment(QTextStream::AlignLeft);
        out.setFieldWidth(5);

        out << "1" << "A" << endl;
        out << "2" << "B" << endl;
        out << "3" << "C" << endl;
        out << "4" << "D" << endl;
        out << "5" << "E" << endl;
        out << "6" << "F" << endl;
    }

    return a.exec();
}

Here is the output generated by the above program:

1    A    
    2    B    
    3    C    
    4    D    
    5    E    
    6    F    

Notice the indentation on the bottom five lines? That's my problem, is I don't want the indentation to start new lines. I want every line to begin at column 0, and to write a value that is five columns wide (my field width) and for it to be left-justified.

I've tried playing around with the parameters for sometime without luck. If I can get this to work in the little test program above, I think I can have success porting that change into my much larger program that writes a text file.

Upvotes: 2

Views: 3757

Answers (2)

Nejat
Nejat

Reputation: 32635

One way of overcoming this is to use qSetFieldWidth before and after writing endl. Setting width to zero before writing endl and again setting to the previous value.

It will not change your code much because of the convenient qSetFieldWidth function which is equivalent to QTextStream::setFieldWidth :

#include <QCoreApplication>
#include <QTextStream>
#include <QFile>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QFile file("testfile.txt");

    if (file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        QTextStream out(&file);

        out.setFieldAlignment(QTextStream::AlignLeft);
        out.setFieldWidth(5);

        out << "1" << "A" << qSetFieldWidth(0) << endl <<qSetFieldWidth(5);
        out << "2" << "B" << qSetFieldWidth(0) << endl <<qSetFieldWidth(5);
        out << "3" << "C" << qSetFieldWidth(0) << endl <<qSetFieldWidth(5);
        out << "4" << "D" << qSetFieldWidth(0) << endl <<qSetFieldWidth(5);
        out << "5" << "E" << qSetFieldWidth(0) << endl <<qSetFieldWidth(5);
        out << "6" << "F" << qSetFieldWidth(0) << endl <<qSetFieldWidth(5);
    }

    return a.exec();
}

Now the result is :

1    A    
2    B    
3    C    
4    D    
5    E    
6    F  

Upvotes: 3

Pavel Strakhov
Pavel Strakhov

Reputation: 40492

From QTextStream::setFieldWidth documentation:

Note: The field width applies to every element appended to this stream after this function has been called (e.g., it also pads endl).

Clearly this is the cause of your issue: endl symbol is padded with 4 spaces just as any other character you put in the stream. A possible workaround is to set field width to 0 before adding endl and resetting field width after that, or to disable stream padding completely and rely on other ways of padding like QString::leftJustified.

Upvotes: 2

Related Questions