Results 1 to 13 of 13

Thread: QTextEdit in QGraphicsScene: How get text printed in correct point size?

  1. #1
    Join Date
    May 2009
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default QTextEdit in QGraphicsScene: How get text printed in correct point size?

    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:

    Qt Code:
    1. // Create scene (ratio of DIN A4)
    2. scene = new QGraphicsScene();
    3. scene->setSceneRect(0, 0, 210, 297);
    4.  
    5. // Set scene in view
    6. ui->graphicsView->setScene(scene);
    7. ui->graphicsView->setSceneRect(0, 0, 210, 297);
    8.  
    9. // Populate scene
    10. QTextEdit *pTextEdit = new QTextEdit(tr("Hallo <b>Fett</b>"));
    11. pTextEdit->setFontPointSize(10);
    12. pTextEdit->setFontFamily("Arial");
    13. pTextEdit->setGeometry(QRect(20, 20, 80, 20));
    14. QGraphicsProxyWidget *pProxy = scene->addWidget(pTextEdit);
    To copy to clipboard, switch view to plain text mode 

    Thanks for any help!

    bye,
    Fabian

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextEdit in QGraphicsScene: How get text printed in correct point size?

    Can't you use QGraphicsTextItem instead of QTextEdit? It would be much easier to control the size of the text...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2009
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextEdit in QGraphicsScene: How get text printed in correct point size?

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextEdit in QGraphicsScene: How get text printed in correct point size?

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    May 2009
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextEdit in QGraphicsScene: How get text printed in correct point size?

    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!

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextEdit in QGraphicsScene: How get text printed in correct point size?

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    fabianr (30th May 2009)

  8. #7
    Join Date
    May 2009
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextEdit in QGraphicsScene: How get text printed in correct point size?

    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?

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextEdit in QGraphicsScene: How get text printed in correct point size?

    Yes, that's correct.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #9
    Join Date
    May 2009
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextEdit in QGraphicsScene: How get text printed in correct point size?

    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?

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextEdit in QGraphicsScene: How get text printed in correct point size?

    Yes, it should be that way. Pixel should be device independent.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #11
    Join Date
    May 2009
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextEdit in QGraphicsScene: How get text printed in correct point size?

    ... 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.

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextEdit in QGraphicsScene: How get text printed in correct point size?

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #13
    Join Date
    May 2009
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextEdit in QGraphicsScene: How get text printed in correct point size?

    Yep, get it. Thanks a lot for your help!!

Similar Threads

  1. Doubt about QTextEdit and Rich Text
    By iamjayanth in forum Qt Programming
    Replies: 1
    Last Post: 8th April 2009, 09:11
  2. Problem pasting text into a QTextEdit
    By Spockmeat in forum Qt Programming
    Replies: 8
    Last Post: 14th March 2009, 15:36
  3. QTextEdit slow to insert text
    By thomaspu in forum Qt Programming
    Replies: 4
    Last Post: 10th January 2008, 13:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.