QCustomPlot Discussion and Comments

Fixing HiDPI printing problems on WindowsReturn to overview

No matter what I try, the axis labels are drawn very large and are cutoff in the output to a printer. I am using the recommended code for rendering. Please note that only Windows HiDPI (e.g. 200% display scaling) suffers from this issue, the identical code on Linux (KDE Plasma) and macOS (Retina displays) Qt distributions works perfectly.

I noticed in the code:

void QCustomPlot::setBufferDevicePixelRatio(double ratio)
  if (!qFuzzyCompare(ratio, mBufferDevicePixelRatio))
    mBufferDevicePixelRatio = ratio;
    foreach(QSharedPointer<QCPAbstractPaintBuffer> buffer, mPaintBuffers)
        // Note: axis label cache has devicePixelRatio as part of cache hash, so no need to manually clear cache here
    qDebug() << Q_FUNC_INFO << "Device pixel ratios not supported for Qt versions before 5.4";
    mBufferDevicePixelRatio = 1.0;

I think the axis label cache may NOT have the correct pixel ratio? How do I "manually clear" the labels cache? I thought this might be a hint since it is only the labels that seem unable to draw correctly.

This is a longstanding bug with Windows HiDPI and output to a printer with QCustomPlot.

Here is a screencap of the printer preview showing the bug:


I have the same problem, Windows HiDPI (e.g. 200% display scaling) only show half chart

The only workaround I have found is to use the device pixel ratio to manually scale the fonts when rendering for printing:

qreal pixelRatio = QGuiApplication::primaryScreen()->devicePixelRatio();

So, for an axes label font, I do the following:

QFont labelFont = ui.plotWidget->yAxis->labelFont();
labelFont.setPointSizeF((labelFontSize = labelFont.pointSizeF()) / pixelRatio);

When exiting the rendering method, I have to reset the fonts back to normal. Some margins, such as the legend width, have to be manually scaled as well.

It should be noted that none of this is necessary or a problem on Linux and macOS for the same versions of Qt.