Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get the font size (in pxl) of a text with paragraphs?
Forum Updated to NodeBB v4.3 + New Features

How to get the font size (in pxl) of a text with paragraphs?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 967 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • NiagarerN Offline
    NiagarerN Offline
    Niagarer
    wrote on last edited by Niagarer
    #1

    Hey,
    I already saw, that I can use QFontMetrics for a single line.
    QFontMetrics tries to do it all in one line.
    But I have a text with paragraphs and have to figure out, how big the rectangle has to be.
    How can I do this?
    Thanks for answers

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by A Former User
      #2

      Hi! In my experience this is a very tricky thing. If your intention is to draw the text with QPainter, I'd recommend to drop QFontMetrics and instead render the text to an "offscreen" image first to get the bounding rect. Something like:

      static QRectF getBoundingBox(QString const &s, QFont const &f)
      {
          QImage image(1, 1, QImage::Format_Mono);
          QPainter painter(&image);
          painter.setFont(f);
          QRectF rr;
          painter.drawText(QRectF(0, 0, INT_MAX, INT_MAX), Qt::TextWordWrap, s, &rr);
          return rr;
      }
      
      1 Reply Last reply
      3

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved