Printing QGraphicsScene Font Size Issue
-
Very simple example. I've made a QGraphicsObject based class with the painter event as:
void MyGfxObj::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { QRectF rect(10.0,10.0, 100.0,100.0); QFont font("Arial"); font.setPointSizeF(12.0); painter->setFont(font); painter->drawText(rect, "Test"); painter->drawRect(rect); }
And that renders fine on-screen. But when I try to render the graphics scene to a printer
void MainWindow::print() { QPrinter printer(QPrinter::HighResolution); printer.setPageSize(QPrinter::A4); QPrintDialog *dialog = new QPrintDialog(&printer, this); if (dialog->exec() == QDialog::Accepted) { QPainter painter(&printer); scene.render(&painter); } }
The text appears completely the wrong size (much bigger).
Why isn't the text being scaled to the device correctly? -
Very simple example. I've made a QGraphicsObject based class with the painter event as:
void MyGfxObj::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { QRectF rect(10.0,10.0, 100.0,100.0); QFont font("Arial"); font.setPointSizeF(12.0); painter->setFont(font); painter->drawText(rect, "Test"); painter->drawRect(rect); }
And that renders fine on-screen. But when I try to render the graphics scene to a printer
void MainWindow::print() { QPrinter printer(QPrinter::HighResolution); printer.setPageSize(QPrinter::A4); QPrintDialog *dialog = new QPrintDialog(&printer, this); if (dialog->exec() == QDialog::Accepted) { QPainter painter(&printer); scene.render(&painter); } }
The text appears completely the wrong size (much bigger).
Why isn't the text being scaled to the device correctly? -
Check QFontMetrics and this
-
@Ni.Sumi I know how to get the metrics of the font, what I don't understand is why a 12pt font is rendered at over 72pt when printing
To be explicit; why does .setPointSizeF(12.0) not result in a 12pt font when printing?
-
I am also having this problem with Qt-5.8.
-
Easiest explanation would be a wrong dpi resolution. Dump printer.resolution() and check whether it makes sense for the printer you have. You can also try to change the DPI setting using printer.setResolution(dpi)