Results 1 to 3 of 3

Thread: Text size

  1. #1
    Join Date
    Jan 2008
    Posts
    155
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Text size

    I want to draw a QGraphicsItem rectangle in QGraphicsScene / QGraphicsView framework, and inside the rectangle I want to draw some identifier text which is, say, 50%, of the size of the rectangle itself. I.e. the text size should scale to the rectangle size.

    How can I do that?

    I tried QFont::setPixelSize(size), but it does NOT work:

    Qt Code:
    1. void
    2. ChipItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    3. QWidget *widget)
    4. {
    5. qreal width, height, size;
    6. QFont monaco("Monaco");
    7. QString pindex;
    8. painter->drawRect(boundingRect());
    9. width = widget->width();
    10. height = widget->height();
    11. if(height>=width) size = width; else size = height;
    12. size /= 2;
    13. monaco.setPixelSize((int)size);
    14. pindex = "P("+pindex.setNum(patindex)+")";
    15. painter->setFont(monaco);
    16. painter->drawText(boundingRect(),Qt::AlignHCenter,pindex);
    17. }
    To copy to clipboard, switch view to plain text mode 

    Any suggestions?
    MacOSX user dabbling with Linux and Windows.

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Text size

    strange calculation... the pixel size (of the font) is the size of one character (or so), certainly not of the whole text or so.

    try something like this

    Qt Code:
    1. QFont font("monaco");
    2. int pixelSize=20;
    3. while (pixelSize>5 && QFontMetrics(font).boundingRect(yourText).size()>yourItemsSize)
    4. {
    5. --pixelSize;
    6. font.setPixelSize(pixelSize);
    7. }
    To copy to clipboard, switch view to plain text mode 

    Untried. Maybe there is a better way to find the maximum font size such that a given text fits a certain rectangle.

    HTH

  3. #3
    Join Date
    Jan 2008
    Posts
    155
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Text size

    I'll try it. But it looks like it could add some overhead if I have many rectangles in my picture.

    strange calculation... the pixel size (of the font) is the size of one character (or so), certainly not of the whole text or so.
    Yes, sorry about being unclear. I meant the text height, primarily. My string would be very short, max 3-4 characters.
    MacOSX user dabbling with Linux and Windows.

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 15:30
  3. QLabel, large, rich text, font size
    By TheKedge in forum Qt Programming
    Replies: 3
    Last Post: 5th February 2007, 11:56
  4. Replies: 1
    Last Post: 24th October 2006, 16:40
  5. Qt 4.1.1 linker warnings
    By Matt Smith in forum Installation and Deployment
    Replies: 0
    Last Post: 26th February 2006, 22:14

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.