PDA

View Full Version : QTextEdit in QGraphicsScene: How get text printed in correct point size?



fabianr
29th May 2009, 23:28
Hi there,

I have a QGraphicsScene with an embedded QTextEdit widget. How do I have to configure the scene so that the text gets the expected size when being printed on paper?

As far as I can see the TextEdit widget takes a point size for the font, but the actually printed size of this text depends on the scene size. The smaller I set the scene size, the larger the text will be printed.

I know that one point corresponds to 1/72 inch. But I have no idea how the point corresponds to the geometry of the text widget (and thus to the scene coordinate system).

Here is how I currently setup the scene:



// Create scene (ratio of DIN A4)
scene = new QGraphicsScene();
scene->setSceneRect(0, 0, 210, 297);

// Set scene in view
ui->graphicsView->setScene(scene);
ui->graphicsView->setSceneRect(0, 0, 210, 297);

// Populate scene
QTextEdit *pTextEdit = new QTextEdit(tr("Hallo <b>Fett</b>"));
pTextEdit->setFontPointSize(10);
pTextEdit->setFontFamily("Arial");
pTextEdit->setGeometry(QRect(20, 20, 80, 20));
QGraphicsProxyWidget *pProxy = scene->addWidget(pTextEdit);

Thanks for any help!

bye,
Fabian

wysota
30th May 2009, 01:51
Can't you use QGraphicsTextItem instead of QTextEdit? It would be much easier to control the size of the text...

fabianr
30th May 2009, 09:13
Thanks for the prompt response!

Yes, I probably can also use QGraphicsTextItem.

So, how do I setup a QGraphicsScene with a QGraphicsTextItem instance in such a way the configured font size (in the QGraphicsTextItem) also has the expected physical size after printing (i.e. my 12pt Arial text has the same size after printing as the 12pt Arial text written in Word)?

Thanks in advance for any help.

wysota
30th May 2009, 09:22
Set the scene rect to the size of the paper you want to print on (i.e. 297x210 for A4). Then set the size of the text in the item to the size in milimetres that you want to obtain. If you prefer picas then set the size of your page in picas.

fabianr
30th May 2009, 10:07
Sorry for annoying, I am a newby to Qt.

How do I set the font size of the text in millimeters? I see that QGraphicsTextItem takes a QFont, which allows me to set the font size in points again.

Is the point size of the font then also in graphics scene space, meaning that I can simply compute it based on graphics scene space coordinates (millimeters)?

If yes, this means that 10 points would be 3.527 units in the graphics scene space (millimeter). Will I run into any problems with these small floating points font sizes?

Background info: I basically want to place a QGraphicsTextItem as a text frame in my document (graphics scene). The size of the QGraphicsTextItem itself thus has to be independent from the font size (as in any word processor).

Thanks a lot. I really appreciate your help!

wysota
30th May 2009, 12:48
In graphics scene the font you set is measured not in screen points but in scene units. If you set the scene height to "100" and you use font size of "10" then each line will take 10% of the scene height. If you set the size of the scene height 297 and then print it on a A4 paper, each unit will be equivalent to 1mm of the printout. If you're worried about floating point font sizes then simply set a scene size that will let you use integer font sizes.

fabianr
30th May 2009, 12:51
Ah, thanks.

So does this mean that QFont::setPointSize actually sets the font size in scene units instead of screen points when working in a graphics scene?

wysota
30th May 2009, 13:11
Yes, that's correct.

fabianr
30th May 2009, 13:28
Hmm. It looks like QFont::setPointSize somehow takes the DPI-resolution of the device into account. The font has a different size on different machines, although I set a fixed scene and font size (the two machines return different QPaintDevice::logicalDpiX/Y values).

It works correctly when setting the font size with the QFont::setPixelSize function. Both machines print the text in the same size then. It seems that one "pixel" then corresponds to one scene unit.

Does this make any sense?

wysota
30th May 2009, 13:55
Yes, it should be that way. Pixel should be device independent.

fabianr
30th May 2009, 14:00
... although the documentation says the opposite for setPixelSize:

"Using this function makes the font device dependent. Use setPointSize() or setPointSizeF() to set the size of the font in a device independent manner."

Confusing.

wysota
30th May 2009, 14:28
I meant using a pixel size will give you the same physical size of the font regardless of the device you use. 12px will always look the same regardless of the device. So the font size is device independent but the size of the pixel obviously is. I just used the wrong words in the previous post.

fabianr
30th May 2009, 14:29
Yep, get it. Thanks a lot for your help!!