How to align QTextEdit text

邮差的信 提交于 2020-01-06 03:13:45

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!