How to efficiently create a fixed width column logger using QPlaintTextEdit?

821 views Asked by At

I want to create a log window in Qt (4.8) using QPlainTextEdit. This means I'll be using appendPlainText. That log needs to display columns of data which need to have a fixed width independently of the data shown (they will have the maximum size of the data). IOW I want them to be able to show something like this:

column 1    column 2    column3
data1       data2       data3
   data1      data2     data3
a           b           c

As you may see in the above example, each column have a fixed width such as if some data is smaller then the available space, the next data will start in the exact starting point of it respective column (instead of just after it). I need to know how to do this in Qt and in an efficient way.

After doing some research on the web, I found some methods I took to be inappropriate. The first is using QString::args():

ui->plainTextEdit->appendPlainText(QString("%1 %2 %3").arg("123456789",15,' ').arg("123456789",15,' ').arg("123456789",15,' '));
ui->plainTextEdit->appendPlainText(QString("%1 %2 %3").arg("12345",15,' ').arg("123456789",15,' ').arg("123456789",15,' '));
ui->plainTextEdit->appendPlainText(QString("%1 %2 %3").arg("333333333333",-15,' ').arg("12345",-15,' ').arg("12345",-15,' '));
ui->plainTextEdit->appendPlainText(QString("%1 %2 %3").arg("333333333333333",15,' ').arg("333333333333333",15,' ').arg("123456789",15,' '));

The above code has some deficiencies: first its default display configuration is with centralized data unless a "-" is set in the place where one tells the size of the column. (the documents officially tells that a positive number gives a right-aligned value, but my tests showed otherwise - see below) This, though, is buggy and limited: I wasn't able to do it right-aligned and if I tell the first arg to have left-aligned texts, then all the other following args display left-aligned text as well even though their column-width number is postive. Furthermore it only actually works if the font style is Monospace.

Another way I discovered is by using QTextStream:

QString s;
QTextStream ss(&s);
ss.setFieldAlignment(QTextStream::AlignLeft);
ss.setFieldWidth(40);

or

QString s;
QTextStream ss(&s);
ss << left << qSetFieldWidth(40) << "Value" << "Keyword/Constant" << qSetFieldWidth(0) << endl;
ss << qSetFieldWidth(40) << "One" << "One" << qSetFieldWidth(0) << endl;

But this seems to be "overly complicated"; I was expecting a solution more like args.

Isn't there another way?

Here is my test with the args code:

Test result for the first code

References: Link 1, Link 2

1

There are 1 answers

0
droid192 On

on pyside2 i use font-familiy monospace (set in qt designer) and then
text_row1 = str(col1).ljust(10) + ...