Setting up the QPrinter:
By default, a QPrinter
object is initialized to screen resolution (usually 96 DPI) unless you specify QPrinter::HighResolution
in the constructor which will then use the resolution of the printer in use.
If you are setting up the QPrinter
object using a QPrintDialog
then the code should be something like this:
QPrinter printer(QPrinter::HighResolution);
QPrintDialog dialog(&printer, this);
dialog.exec();
std::cout << printer.resolution() << std::endl;
After this, the program should output the DPI of the selected printer. In my case it prints out 600.
If you aren't using the QPrintDialog
, you should use the QPrinter
constructor as shown above and then call setResolution(DPI)
with the known DPI of your printer.
This should result in fonts that are rendered correctly.
Update:
Now that the weekend is here, I finally had time to properly consider this issue :)
Although technically correct for setting up a QPrinter, the above solution is not practical for Graphics scenes that include text specified in point sizes. Since all graphic items are specified in pixel coordinates, it only makes sense to specify font sizes in pixels as well to ensure that fonts appear exactly as expected when mixed with other graphic primitives.
There is no need to be concerned about the size of the text on different monitors as the graphics items themselves are not resolution independent. The view can specify scale translations to deal with different resolution and DPI monitors.
When printing, by default, the QPrinter scales to fit the entire scene to the page. Which makes sense since a 100 x 100 square on a 600 DPI printer is goint to be 1/6th of an inch wide on your paper :)