Qt4/PyQt4 - 无法为 QTextDocument 设置默认字体 [英] Qt4/PyQt4 - Can not set the default font for QTextDocument
问题描述
我的代码是这样的:
from PyQt4 import QtGui
doc = QtGui.QTextDocument()
d_font = QtGui.QFont('Times New Roman')
doc.setDefaultFont(d_font)
cur = QtGui.QTextCursor(doc)
cur.insertText('sample text')
writer = QtGui.QTextDocumentWriter()
writer.setFormat(writer.supportedDocumentFormats()[1])
writer.setFileName('CV')
writer.write(doc)
输出中的示例文本"在我的电脑上仍然是Sans",而不是Times New Roman".我已经确保我的电脑有Times New Roman"字体.我怀疑这是一个错误.我'm 使用 PyQt v4.9.5.
The 'sample text' in the output is still 'Sans' on my computer rather than "Times New Roman'. I have made sure my computer has 'Times New Roman' font. I suspect this is a bug. I'm using PyQt v4.9.5.
我使用的是 Ubuntu 12.04.我很确定 PyQt4 可以找到字体,因为以下代码有效:
I'm using Ubuntu 12.04. I'm quite sure that PyQt4 can find the font, because the following code works:
d_font = QFont('Times New Roman')
char_fmt = QTextCharFormat()
char_fmt.setFont(d_font)
cur.insertText('Times New Roman', char_fmt)
<小时>
以 odt/odt 格式保存时,似乎并非所有格式都受支持,但在打印为 pdf 时一切正常.
It appears that not all formating is supported when saving in odt/odt format, but everything works as expected when printing to a pdf.
from PyQt4.QtGui import *
import sys
doc = QTextDocument()
cur = QTextCursor(doc)
d_font = QFont('Times New Roman')
doc.setDefaultFont(d_font)
table_fmt = QTextTableFormat()
table_fmt.setColumnWidthConstraints([
QTextLength(QTextLength.PercentageLength, 30),
QTextLength(QTextLength.PercentageLength, 70)
])
table = cur.insertTable(5,2, table_fmt)
cur.insertText('sample text 1')
cur.movePosition(cur.NextCell)
cur.insertText('sample text 2')
# Print to a pdf file
# QPrinter: Must construct a QApplication before a QPaintDevice
app = QApplication(sys.argv)
printer = QPrinter(QPrinter.HighResolution)
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName('sample.pdf')
# Save to file
writer = QTextDocumentWriter()
writer.setFormat(writer.supportedDocumentFormats()[1])
writer.setFileName('sample.odt')
writer.write(doc)
setDefaultfonts 和 setColumnWidthConstraints 影响 sample.pdf,但不影响 sample.odt.
setDefaultfonts and setColumnWidthConstraints affect sample.pdf, but not sample.odt.
推荐答案
我可以在使用 PyQt4.8.5 的 Ubuntu Oneiric 机器上看到相同的行为.我不认为这是一个错误.书写文本的字体取决于用于书写文本的光标字体.
I can see the same behavior on my Ubuntu Oneiric box with PyQt4.8.5. I don't think it is a bug. The font of the written text depends on the font of the cursor used to write the text.
以下应该对您有用:
from PyQt4 import QtGui
doc = QtGui.QTextDocument()
cur = QtGui.QTextCursor(doc)
d_font = QtGui.QFont('Courier')
c_format = QtGui.QTextCharFormat()
c_format.setFont(d_font)
cur.setCharFormat(c_format)
cur.insertText('sample text')
writer = QtGui.QTextDocumentWriter()
writer.setFormat(writer.supportedDocumentFormats()[1])
writer.setFileName('CV')
writer.write(doc)
我使用了 Courier,因为我的系统上没有安装 Times New Roman.
I've used Courier because Times New Roman is not installed on my system.
这篇关于Qt4/PyQt4 - 无法为 QTextDocument 设置默认字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!