QPrinter resolution is not correct on Linux

I am trying to get an image printing program to work in Qt. Trying to print to a custom printer that has ppd. There are calculations based on device information to generate an image to be sent to the printer.

When I look at the properties of the printer, I see that Resolution

- 300 dpi x 300 dpi


On Windows, everything works fine - but on Linux, the computed image information gets very large, causing my files to break ... Going through all the data, I found that on Linux physicalDpiX

and physicalDpiY

(used in the code) is 1200 instead of 300.

So I blame it on QPrinter :: PrinterMode

QPrinter :: HighResolution 2 In Windows, the printer resolution is set which are specific to the printer being used. For PostScript printing, sets the resolution of the PostScript driver to 1200 dpi.

I changed my constructor to take care of this - just in case the defaults are wrong ... It didn't work:

Printer::Printer(const QPrinterInfo& printerInfo, MainWindow* pWnd) :
#if defined(Q_OS_WIN32) || defined (Q_MAC_OSX)
  qDebug()<<"printer resolution physicalDpiX="<< this->physicalDpiX()<<", physicalDpiY="<<this->physicalDpiY();
  // prints 1200 for each in Linux, 300 in windows
  qDebug()<<"printer resolution="<< this->resolution();
  // prints 96 in Linux, 300 in windows
  // printer properties (like from system-config-printer) show 300
  // printerInfo.printerName() and printerInfo.defaultPrinter().printerName() show my printer


numbers are wrong for resolution on Linux, but fixed on Windows


this-> setResolution (300);

This made this.resolution () equal to 300, but the physicalDpiX and Y still show 1200.

QPrinter :: supportedResolutions () seems to be saying it's hopeless ...

How do I make my printer the display resolution specified in the printer properties?

Is there only one hope of getting some methods out of the cups? I tried ... I don't understand how to use only the information I found: Cups resolution


source to share

2 answers

As much as I would like a general answer to this question, for now I have solved my problem for the specific drivers that the user should be using, leaving the default (1200) for others like pdf or hp printing ..

My custom printer contains, among the cup options, a resolution option that looks something like

name="printer-resolution" value="300x300dpi"


I can extract permission from it. If this special cups parameter does not exist, I can use 1200 as the default.



First, never rely on the resolution of physical devices. The user can use printers with different resolutions, print once at one resolution (300 dpi), next time at a different resolution (600 dpi), or print to a PDF file, or open a preview window that uses the screen resolution ... Secondly, use ScreenResolution only for screen printing, which is too rough for any high-resolution device. The letters and images will be ugly and the positioning on the page will be too low.

Printing regardless of the resolution of the physical device and operating system can also be achieved with QPainter scaling. As far as I know, the base resolution of the QPainter is 1200dpi. This means that this is the best resolution Qt can print. The following example shows how to set scaling before drawing any content on the page.

QPrinter printer(QPrinter::HighResolution);
qreal resolutionFactor = 1200 / printer->resolution();
QPainter painter;
painter.scale(1 / resolutionFactor, 1 / resolutionFactor);
printPage(&painter); // This method should implement printing itself


Then paint the content (set the x and y coordinates, width and height for images, etc.) for a base QPainter resolution of 1200 dpi and the rendering process will take care that all dimensions are automatically scaled to the target device resolution.



All Articles