[SOLVED] How to properly get font height in QPainter



  • 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 :)



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.