PyQt中QTextEdit的文本格式化
我刚开始学习PyQt编程,使用PyQt4设计器开发了一个图形界面,界面看起来像图片中的样子:

当我点击check_inertia按钮时,QTextEdit这个小部件会显示出图片中所示的文本。我们可以看到,这些文本看起来有点乱,我需要控制QTextEdit的格式。
check_inertia按钮会读取一个.txt文件的内容,然后把以下信息打印到QTextEdit小部件中,这个文本文件的格式大概是这样的:
* ============================== INERTIA ==============================
* File: /home/hamanda/Desktop/transfer/cradle_vs30_dkaplus01_fwd_dl140606_fem140704_v00.bif
* Solver: Nastran
* Date: 02/09/14
* Time: 08:35:30
* Text:
*
* Area +1.517220e+06
* Volume +5.852672e+06
*
* Structural mass +4.594348e-02
* MASS elements +0.000000e+00
* NSM on property entry +0.000000e+00
* NSM by parts (VMAGen and MPBalanc) +0.000000e+00
* NSM by NSMCreate +0.000000e+00
* Total mass +4.594348e-02
*
* Center of gravity
* in the global +1.538605e+02 +3.010898e+00 -2.524868e+02
* coordinate system
*
* Moments of inertia +8.346990e+03 +6.187810e-01 +1.653922e+03
* about the global +6.187810e-01 +5.476398e+03 +4.176218e+01
* coordinate system +1.653922e+03 +4.176218e+01 +7.746156e+03
*
* Steiner share +2.929294e+03 +4.016500e+03 +1.088039e+03
*
* Moments of inertia +5.417696e+03 +2.190247e+01 -1.308790e+02
* about the center +2.190247e+01 +1.459898e+03 +6.835397e+00
* of gravity -1.308790e+02 +6.835397e+00 +6.658117e+03
* ---------------------------------------------------------------------
那么,我该如何让QTextEdit中的文本格式和上面看到的一样呢?我应该使用样式表吗?请帮帮我。
1 个回答
0
请使用 monospace
或 consola
字体,或者其他在控制台或编程中常用的字体。你可以在样式表中设置这些字体;
QTextEdit {
font: 10pt "Consolas";
}
放入你的 QTextEdit
中;
yourQTextEdit.setStyleSheet ('''
QTextEdit {
font: 10pt "Consolas";
}
''')
注意:QPlainTextEdit
也可以使用这个字体,我推荐用它来显示纯文本,并且它的功能和 QTextEdit
是一样的。