问题
How can I align this text? \t
is not good.
What I see (image)
Expected result (image)
回答1:
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.
回答2:
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
来源:https://stackoverflow.com/questions/51784074/how-to-align-qtextedit-text