7

How do I convert my font on a QGraphicsObject from point size to pixel size? I need to do this so the fonts will look right when I print my QGraphicsScene using QGraphicsScene::render().

David Burson
  • 2,947
  • 7
  • 32
  • 55
  • What are you looking for in the end result? Why doesn't the default `render()` call work for you? – Kaleb Pederson Sep 13 '10 at 17:31
  • I want the scene to be printed on paper, and look like it does on the screen. The default `render` produces correct results for everything except text. My fonts are specified in points, and the default `render()`, when I pass it a `QPainter` created from my `QPrinter`, makes the font huge. Here's my original question: http://stackoverflow.com/questions/3679837/how-to-print-a-qgraphicsscene-that-has-text-and-graphics – David Burson Sep 13 '10 at 17:57

1 Answers1

10

Probably class QFontMetrics will do the job. Just create your desired QFont, set It's point size. Then create QFontMetrics object on your QFont.

Kamil Klimek
  • 12,884
  • 2
  • 43
  • 58
  • 1
    I'm having trouble figuring out how to use QFontMetrics to do this. I've also looked at QFontInfo, which does give me the current pixel size, but I'm not sure what to do with it. So far, font.setPixelSize(font.pointSize()) seems to work, but I don't know why - doesn't seem like it will be reliable. Thoughts? – David Burson Sep 13 '10 at 17:19
  • font.setPointSize(yourPointSize); QFontMetrics fm(font); fm.height() <- this will give you font height in pixels. Point size is DPI dependent. So probably at your dpi 1px is near 1point size. – Kamil Klimek Sep 14 '10 at 19:06