Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8655941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:09:30+00:00 2026-06-12T15:09:30+00:00

My code is like this: from PyQt4 import QtGui doc = QtGui.QTextDocument() d_font =

  • 0

My code is like this:

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)

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.

EDIT: 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)

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 and setColumnWidthConstraints affect sample.pdf, but not sample.odt.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:09:30+00:00Added an answer on June 12, 2026 at 3:09 pm

    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.

    The following should work for you:

    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)
    

    I’ve used Courier because Times New Roman is not installed on my system.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I saw a piece of code which was written like this: if (from n
I have several HTML code combinations like this: <h2></h2> <p></p> <iframe></iframe> <hr/> and from
I'm automating Visio 2003 from a VB.NET app. My code looks like this (with
The Problem I'm receiving crash reports from users that look like this: Code Type:
I have this source code from 2001 that I would like to compile. It
I'd like to call a Clojure function from Java code. This question hasn't been
How to build a .bundle from source code? This might sound like a simple
Code like this works well. Intent configIntent = new Intent (context, WidgetConfigActivity.class); configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Given code like this from a node.js neophyte like me: require('http').createServer(function( req, resp )
I can read all column Data from code like this ... FMResultSet *rs =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.