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. [SOLVED] How to properly get font height in QPainter
Qt 6.11 is out! See what's new in the release blog

[SOLVED] How to properly get font height in QPainter

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 1.9k Views 1 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.
  • I Offline
    I Offline
    i92guboj
    wrote on last edited by
    #1

    Hello.

    I am trying to create some (Spartan, I'd say) reports using QPrinter/QPrinter.

    To be able to track the current location I just use a variable, which is conveniently updated, and then reused in the next painting. For the purpose of this post, that'll be the next drawText() sentence.

    So, imagine we have this snippet:

    @ painter.setFont(fontH1);
    painter.drawText(0, current_y, printer.width()-header_pixmap->width(), QFontInfo(fontH1).pixelSize(),
    Qt::AlignLeft, datos_operador.nombre);
    current_y += QFontInfo(fontH1).pixelSize();
    painter.setFont(fontH4);
    painter.drawText(0, current_y, printer.width()-header_pixmap->width(), QFontInfo(fontH4).pixelSize(),
    Qt::AlignLeft, datos_operador.codigo);
    current_y += QFontInfo(fontH4).pixelSize();
    painter.drawText(0, current_y, printer.width()-header_pixmap->width(), QFontInfo(fontH4).pixelSize(),
    Qt::AlignLeft, datos_operador.direccion);
    current_y += QFontInfo(fontH4).pixelSize();
    painter.drawText(0, current_y, printer.width()-header_pixmap->width(), QFontInfo(fontH4).pixelSize(),
    Qt::AlignLeft, datos_operador.localidad);
    current_y += QFontInfo(fontH4).pixelSize();
    painter.drawText(0, current_y, printer.width()-header_pixmap->width(), QFontInfo(fontH4).pixelSize(),
    Qt::AlignLeft, datos_operador.telefonos);
    current_y += QFontInfo(fontH4).pixelSize();
    painter.drawText(0, current_y, printer.width()-header_pixmap->width(), QFontInfo(fontH4).pixelSize(),
    Qt::AlignLeft, datos_operador.web);
    current_y += QFontInfo(fontH4).pixelSize();
    painter.drawText(0, current_y, printer.width()-header_pixmap->width(), QFontInfo(fontH4).pixelSize(),
    Qt::AlignLeft, datos_operador.email);@

    Easy enough to understand. The problem here is that letters like "g", "j", "y" or "p" always get cut on the cut on the bottom side.

    I have also tried to use QFontMetrics(font).height() but it doesn't seem to make any (noticeable) difference.

    So, how do people deal with this? I've been reading QFontMetrics() and QFontInfo() manuals, but I can't really make all the sense I'd like from them.

    Thanks for reading and for any hint you can share :)

    1 Reply Last reply
    0
    • I Offline
      I Offline
      i92guboj
      wrote on last edited by
      #2

      Nevermind. I've been looking for this since two months ago, but as always I just found it after posting here.

      See "here":http://qt-project.org/forums/viewthread/11674 about QFontMetrics::boundingRect(). I solved it this way:

      @painter.drawText(0, current_y, printer.width()-header_pixmap->width(),
      QFontMetrics(fontH4).boundingRect(datos_operador.email).height(),
      Qt::AlignLeft, datos_operador.email);@

      Sorry for the noise.

      1 Reply Last reply
      0

      • Login

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