Nick Brian
Nick Brian

Reputation: 53

How to align QTextEdit text

How can I align this text? \t is not good.

What I see (image)

Expected result (image)

Upvotes: 2

Views: 1399

Answers (2)

Phiber
Phiber

Reputation: 1103

this line:

QString str = QString("B%1").arg(_ui->lineEdit->text().toInt(), 3, 10, QChar('0'));

lineEdit->text() = 1 ,    str = B001
lineEdit->text() = 01 ,   str = B001
lineEdit->text() = 001 ,  str = B001
lineEdit->text() = 0001 , str = B001
lineEdit->text() = 12 ,   str = B012
lineEdit->text() = 123 ,  str = B123

you can adapt it for your use.

Edit based on Hyde Comment

int main(int argc, char **argv)
{
 qint32 a,b,c,d,e;

a = -1;b = 1;c = -1;d = 3;
e = (a*b) + (c*d);
QString str = QString("(%1*%2) + (%3*%4) = %5").arg(a, 2, 10, QChar(' '))
                                               .arg(b, 2, 10, QChar(' '))
                                               .arg(c, 2, 10, QChar(' '))
                                               .arg(d, 2, 10, QChar(' '))
                                                .arg(e);

QTextStream(stdout) << str << endl;

a = -1;b = 2;c = -1;d = 4;
e = (a*b) + (c*d);
str = QString("(%1*%2) + (%3*%4) = %5").arg(a, 2, 10, QChar(' '))
                                              .arg(b, 2, 10, QChar(' '))
                                              .arg(c, 2, 10, QChar(' '))
                                              .arg(d, 2, 10, QChar(' '))
                                               .arg(e);

QTextStream(stdout) << str << endl;

return 0;
}

the output is:

(-1 * 1) + (-1 * 3) = -4 
(-1 * 2) + (-1 * 4) = -6

Upvotes: 3

Picaud Vincent
Picaud Vincent

Reputation: 10982

I had this problem once in the past. To solve the issue I used monospace font.

To get everything aligned (fixed font width) I used these lines:

  // Use "monospaced" font: 
  // ->insure left column alignment, as for the terminal
  //
  QFont font("monospace");
  font.setPointSize(10);
  myQTextEdit->setCurrentFont(font);

from my parent widget containing a QTextEdit child widget.

Upvotes: 4

Related Questions